Hướng dẫn how do you clear a variable in php? - làm thế nào để bạn xóa một biến trong php?

0

Show

Mới! Lưu câu hỏi hoặc câu trả lời và sắp xếp nội dung yêu thích của bạn. Tìm hiểu thêm.
Learn more.

Tôi biết một vài phương pháp để đặt lại một biến trong PHP.

Vấn đề là tôi không biết chính xác sự khác biệt giữa họ và ai nhanh hơn nên ở đây tôi hỏi ...

Sự khác biệt giữa:


và...

Tôi rất vui khi biết nếu có các thủ thuật khác để loại bỏ hoàn toàn một biến khỏi bộ nhớ.

Hơn nữa, tôi rất muốn biết nếu có bất kỳ lý do nào để đặt lại một biến ở cuối trang kể từ khi máy chủ đặt lại cho họ khi trang được tải đầy đủ ...

Chỉ để làm rõ thêm ...


Cảm ơn trước !

Hỏi ngày 4 tháng 12 năm 2014 lúc 7:50Dec 4, 2014 at 7:50

Hướng dẫn how do you clear a variable in php? - làm thế nào để bạn xóa một biến trong php?

StevenstevenSteven

2395 Huy hiệu bạc14 Huy hiệu Đồng5 silver badges14 bronze badges

$ resetMe = null; - sẽ không xóa VAR khỏi bộ nhớ

unset ($ resetMe); - Xóa var khỏi bộ nhớ

$ resetMe = 0; - sẽ không xóa VAR khỏi bộ nhớ

Để loại bỏ hoàn toàn một biến khỏi bộ nhớ, bạn cần sử dụng Unset. Tất cả các cách khác chỉ thay đổi giá trị biến

Nhưng nếu biến sẽ không tồn tại, bạn sẽ nhận được lỗi trong việc mở khóa này ($ resetMe);

Cách tốt hơn để sử dụng $ resetMe = null;

Sau khi tập lệnh kết thúc, PHP sẽ làm sạch bộ nhớ.

Đã trả lời ngày 4 tháng 12 năm 2014 lúc 7:53Dec 4, 2014 at 7:53

Hướng dẫn how do you clear a variable in php? - làm thế nào để bạn xóa một biến trong php?

OlegolegOleg

6554 Huy hiệu bạc10 Huy hiệu đồng4 silver badges10 bronze badges

2

❮ Tham chiếu xử lý biến PHP

Thí dụ

Biến số không đặt:

Muhamad_zakaria tại Yahoo Dot Com ¶
$a = "Hello world!";
echo "The value of variable 'a' before unset: " . $a . "
";
unset($a);
echo "The value of variable 'a' after unset: " . $a;
?>

Hãy tự mình thử »


Định nghĩa và cách sử dụng

Hàm unset () giải quyết một biến.


Cú pháp

Giá trị tham số

Tham sốSự mô tả
Biến đổiYêu cầu. Chỉ định biến để hủy bỏ
...Không bắt buộc. Một biến khác để hủy bỏ

Chi tiết kỹ thuật

Giá trị trở lại:Không có
Loại trở lại:Không có
Loại trở lại:Phiên bản PHP:

❮ Tham chiếu xử lý biến PHP


Hàm RESET () di chuyển con trỏ bên trong sang phần tử đầu tiên của mảng. Các phương thức liên quan: Dòng điện () - Trả về giá trị của phần tử hiện tại trong một mảng. end () - di chuyển con trỏ bên trong và đầu ra, phần tử cuối cùng trong mảng.

Unset () trong PHP là gì?Unset a given variable

unset () phá hủy các biến được chỉ định. Hành vi của unset () bên trong một hàm có thể thay đổi tùy thuộc vào loại biến bạn đang cố gắng phá hủy. Nếu một biến toàn cầu hóa không được đặt () bên trong một hàm, chỉ có biến cục bộ bị phá hủy.

Làm thế nào để bạn hủy bỏ nhiều giá trị trong PHP?unset() inside of a function can vary depending on what type of variable you are attempting to destroy.

Bạn phải sử dụng cho vòng lặp cho việc này. Bạn có thể sử dụng Foreach Loop nhưng nó sẽ không được đặt tất cả các biến vẫn còn một biến vẫn còn.unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.

function destroy_foo() 
{
    global 
$foo;
    unset(
$foo);
}
$foo 'bar';
destroy_foo();
echo 
$foo;
?>

Sự khác biệt giữa unset () và thiếu liên kết () là gì?

Hàm hủy liên kết () được sử dụng khi bạn muốn xóa hoàn toàn các tệp. Hàm unset () được sử dụng khi bạn muốn làm cho tệp đó trống.unset() a global variable inside of a function, then use the $GLOBALS array to do so:

function foo() 
{
    unset(
$GLOBALS['bar']);
}
$bar "something";
foo();
?>

(Php 4, Php 5, Php 7, Php 8)unset() inside of a function, only the local variable is destroyed. The variable in the calling environment will retain the same value as before unset() was called.

function foo(&$bar
{
    unset(
$bar);
    
$bar "blah";
}
$bar 'something';
echo 
"$bar\n";foo($bar);
echo 
"$bar\n";
?>

Sự khác biệt giữa unset () và thiếu liên kết () là gì?

Hàm hủy liên kết () được sử dụng khi bạn muốn xóa hoàn toàn các tệp. Hàm unset () được sử dụng khi bạn muốn làm cho tệp đó trống.unset() inside of a function, unset() destroys the variable only in the context of the rest of a function. Following calls will restore the previous value of a variable.

function foo()
{
    static 
$bar;
    
$bar++;
    echo 
"Before unset: $bar, ";
    unset(
$bar);
    
$bar 23;
    echo 
"after unset: $bar\n";
}
foo();
foo();
foo();
?>

Sự khác biệt giữa unset () và thiếu liên kết () là gì?

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23

Hàm hủy liên kết () được sử dụng khi bạn muốn xóa hoàn toàn các tệp. Hàm unset () được sử dụng khi bạn muốn làm cho tệp đó trống.

(Php 4, Php 5, Php 7, Php 8)

Unset - Und đặt một biến đã cho

Sự mô tả

Hành vi của unset () bên trong một hàm có thể thay đổi tùy thuộc vào loại biến bạn đang cố gắng phá hủy.

Nếu một biến toàn cầu hóa không được đặt () bên trong một hàm, chỉ có biến cục bộ bị phá hủy. Biến trong môi trường gọi sẽ giữ lại giá trị giống như trước khi unset () được gọi.

Ví dụ trên sẽ xuất ra:

Để giải quyết () một biến toàn cầu bên trong hàm, sau đó sử dụng mảng $ globals để làm như vậy:

Nếu một biến được truyền bởi tham chiếu là unset () bên trong một hàm, chỉ có biến cục bộ bị phá hủy. Biến trong môi trường gọi sẽ giữ lại giá trị giống như trước khi unset () được gọi.unset() example

// destroy a single variable
unset($foo);// destroy a single element of an array
unset($bar['quux']);// destroy more than one variable
unset($foo1$foo2$foo3);
?>

Nếu một biến tĩnh không được đặt () bên trong hàm, unSet () sẽ tiêu diệt biến chỉ trong bối cảnh phần còn lại của hàm. Các cuộc gọi sau đây sẽ khôi phục giá trị trước đó của một biến.

Thông sốunset() function.


0 casting serves only as a

3-type cast, for completeness. It does not alter the variable it's casting. The (unset) cast is deprecated as of PHP 7.2.0, removed as of 8.0.0.


4

Sự khác biệt giữa unset () và thiếu liên kết () là gì?

Hàm hủy liên kết () được sử dụng khi bạn muốn xóa hoàn toàn các tệp. Hàm unset () được sử dụng khi bạn muốn làm cho tệp đó trống.

(Php 4, Php 5, Php 7, Php 8): Because this is a language construct and not a function, it cannot be called using variable functions, or named arguments.

Unset - Und đặt một biến đã cho:

Sự mô tả

Unset - Und đặt một biến đã cho:

Sự mô tả

Unset - Und đặt một biến đã cho:

Sự mô tảunset() on inaccessible object properties, the __unset() overloading method will be called, if declared.

Hành vi của unset () bên trong một hàm có thể thay đổi tùy thuộc vào loại biến bạn đang cố gắng phá hủy.

  • Nếu một biến toàn cầu hóa không được đặt () bên trong một hàm, chỉ có biến cục bộ bị phá hủy. Biến trong môi trường gọi sẽ giữ lại giá trị giống như trước khi unset () được gọi.
  • Ví dụ trên sẽ xuất ra:
  • __unset()
  • Để giải quyết () một biến toàn cầu bên trong hàm, sau đó sử dụng mảng $ globals để làm như vậy:
  • Nếu một biến được truyền bởi tham chiếu là unset () bên trong một hàm, chỉ có biến cục bộ bị phá hủy. Biến trong môi trường gọi sẽ giữ lại giá trị giống như trước khi unset () được gọi.

Hayley Watson ¶

Nếu một biến tĩnh không được đặt () bên trong hàm, unSet () sẽ tiêu diệt biến chỉ trong bối cảnh phần còn lại của hàm. Các cuộc gọi sau đây sẽ khôi phục giá trị trước đó của một biến.


6


7


8


9

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
0

Thông số

14 năm trước

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
1

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
2

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
3

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
4

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
5

var

14 năm trước

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
6

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
7

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
0

Biến không được đặt.

12 năm trước

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
9

function destroy_foo() 
{
    global 
$foo;
    unset(
$foo);
}
$foo 'bar';
destroy_foo();
echo 
$foo;
?>
0

function destroy_foo() 
{
    global 
$foo;
    unset(
$foo);
}
$foo 'bar';
destroy_foo();
echo 
$foo;
?>
1

function destroy_foo() 
{
    global 
$foo;
    unset(
$foo);
}
$foo 'bar';
destroy_foo();
echo 
$foo;
?>
2

________số 8

12 năm trước

function destroy_foo() 
{
    global 
$foo;
    unset(
$foo);
}
$foo 'bar';
destroy_foo();
echo 
$foo;
?>
3

function destroy_foo() 
{
    global 
$foo;
    unset(
$foo);
}
$foo 'bar';
destroy_foo();
echo 
$foo;
?>
4

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
0

macnimble tại gmail dot com

13 năm trước

function destroy_foo() 
{
    global 
$foo;
    unset(
$foo);
}
$foo 'bar';
destroy_foo();
echo 
$foo;
?>
6

function destroy_foo() 
{
    global 
$foo;
    unset(
$foo);
}
$foo 'bar';
destroy_foo();
echo 
$foo;
?>
7

function destroy_foo() 
{
    global 
$foo;
    unset(
$foo);
}
$foo 'bar';
destroy_foo();
echo 
$foo;
?>
8

function destroy_foo() 
{
    global 
$foo;
    unset(
$foo);
}
$foo 'bar';
destroy_foo();
echo 
$foo;
?>
9

function foo() 
{
    unset(
$GLOBALS['bar']);
}
$bar "something";
foo();
?>
0

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
0

Chad 0x40 Herballure 0x2e com ¶

15 năm trước

function foo() 
{
    unset(
$GLOBALS['bar']);
}
$bar "something";
foo();
?>
2

function foo() 
{
    unset(
$GLOBALS['bar']);
}
$bar "something";
foo();
?>
3

function foo() 
{
    unset(
$GLOBALS['bar']);
}
$bar "something";
foo();
?>
4

function foo() 
{
    unset(
$GLOBALS['bar']);
}
$bar "something";
foo();
?>
5

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
0

Tigercat tại Aol Dot Com ¶

18 năm trước

function foo() 
{
    unset(
$GLOBALS['bar']);
}
$bar "something";
foo();
?>
7

function foo() 
{
    unset(
$GLOBALS['bar']);
}
$bar "something";
foo();
?>
8

function foo() 
{
    unset(
$GLOBALS['bar']);
}
$bar "something";
foo();
?>
9

function foo(&$bar
{
    unset(
$bar);
    
$bar "blah";
}
$bar 'something';
echo 
"$bar\n";foo($bar);
echo 
"$bar\n";
?>
0

pfreilly tại umd dot umich dot edu ¶

11 năm trước

function foo(&$bar
{
    unset(
$bar);
    
$bar "blah";
}
$bar 'something';
echo 
"$bar\n";foo($bar);
echo 
"$bar\n";
?>
1

function foo(&$bar
{
    unset(
$bar);
    
$bar "blah";
}
$bar 'something';
echo 
"$bar\n";foo($bar);
echo 
"$bar\n";
?>
2

function foo(&$bar
{
    unset(
$bar);
    
$bar "blah";
}
$bar 'something';
echo 
"$bar\n";foo($bar);
echo 
"$bar\n";
?>
3

function foo(&$bar
{
    unset(
$bar);
    
$bar "blah";
}
$bar 'something';
echo 
"$bar\n";foo($bar);
echo 
"$bar\n";
?>
4

function foo(&$bar
{
    unset(
$bar);
    
$bar "blah";
}
$bar 'something';
echo 
"$bar\n";foo($bar);
echo 
"$bar\n";
?>
5

function foo(&$bar
{
    unset(
$bar);
    
$bar "blah";
}
$bar 'something';
echo 
"$bar\n";foo($bar);
echo 
"$bar\n";
?>
6

function foo(&$bar
{
    unset(
$bar);
    
$bar "blah";
}
$bar 'something';
echo 
"$bar\n";foo($bar);
echo 
"$bar\n";
?>
7

function foo(&$bar
{
    unset(
$bar);
    
$bar "blah";
}
$bar 'something';
echo 
"$bar\n";foo($bar);
echo 
"$bar\n";
?>
8

function foo(&$bar
{
    unset(
$bar);
    
$bar "blah";
}
$bar 'something';
echo 
"$bar\n";foo($bar);
echo 
"$bar\n";
?>
9

function foo()
{
    static 
$bar;
    
$bar++;
    echo 
"Before unset: $bar, ";
    unset(
$bar);
    
$bar 23;
    echo 
"after unset: $bar\n";
}
foo();
foo();
foo();
?>
0

function foo()
{
    static 
$bar;
    
$bar++;
    echo 
"Before unset: $bar, ";
    unset(
$bar);
    
$bar 23;
    echo 
"after unset: $bar\n";
}
foo();
foo();
foo();
?>
1

function foo()
{
    static 
$bar;
    
$bar++;
    echo 
"Before unset: $bar, ";
    unset(
$bar);
    
$bar 23;
    echo 
"after unset: $bar\n";
}
foo();
foo();
foo();
?>
2

function foo()
{
    static 
$bar;
    
$bar++;
    echo 
"Before unset: $bar, ";
    unset(
$bar);
    
$bar 23;
    echo 
"after unset: $bar\n";
}
foo();
foo();
foo();
?>
3

function foo()
{
    static 
$bar;
    
$bar++;
    echo 
"Before unset: $bar, ";
    unset(
$bar);
    
$bar 23;
    echo 
"after unset: $bar\n";
}
foo();
foo();
foo();
?>
4

function foo()
{
    static 
$bar;
    
$bar++;
    echo 
"Before unset: $bar, ";
    unset(
$bar);
    
$bar 23;
    echo 
"after unset: $bar\n";
}
foo();
foo();
foo();
?>
5

function foo()
{
    static 
$bar;
    
$bar++;
    echo 
"Before unset: $bar, ";
    unset(
$bar);
    
$bar 23;
    echo 
"after unset: $bar\n";
}
foo();
foo();
foo();
?>
6

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
0

Clark tại Everettsconsulting Dot Com ¶

17 năm trước

function foo()
{
    static 
$bar;
    
$bar++;
    echo 
"Before unset: $bar, ";
    unset(
$bar);
    
$bar 23;
    echo 
"after unset: $bar\n";
}
foo();
foo();
foo();
?>
8

function foo()
{
    static 
$bar;
    
$bar++;
    echo 
"Before unset: $bar, ";
    unset(
$bar);
    
$bar 23;
    echo 
"after unset: $bar\n";
}
foo();
foo();
foo();
?>
9

var0

var1

Phpmanual tại Kennel17 Dot Co Dot Uk ¶

12 năm trước

var2

var3

var4

var5

var6

Tecdoc tại Ukr Dot Net ¶

5 tháng trước

var7

Ẩn danh ¶

15 năm trước

var8

var9

var4

vars1

vars2

Lion_Cat tại Mail Ru ¶

13 năm trước

vars3

vars4

vars5

vars6

vars7

vars8

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
0

Dan at--nospam-- cubeland dot co dot uk ¶

17 năm trước

// destroy a single variable
unset($foo);// destroy a single element of an array
unset($bar['quux']);// destroy more than one variable
unset($foo1$foo2$foo3);
?>
0

// destroy a single variable
unset($foo);// destroy a single element of an array
unset($bar['quux']);// destroy more than one variable
unset($foo1$foo2$foo3);
?>
1

var0

// destroy a single variable
unset($foo);// destroy a single element of an array
unset($bar['quux']);// destroy more than one variable
unset($foo1$foo2$foo3);
?>
3

Phpmanual tại Kennel17 Dot Co Dot Uk ¶

18 năm trước

// destroy a single variable
unset($foo);// destroy a single element of an array
unset($bar['quux']);// destroy more than one variable
unset($foo1$foo2$foo3);
?>
4

// destroy a single variable
unset($foo);// destroy a single element of an array
unset($bar['quux']);// destroy more than one variable
unset($foo1$foo2$foo3);
?>
5

// destroy a single variable
unset($foo);// destroy a single element of an array
unset($bar['quux']);// destroy more than one variable
unset($foo1$foo2$foo3);
?>
6

// destroy a single variable
unset($foo);// destroy a single element of an array
unset($bar['quux']);// destroy more than one variable
unset($foo1$foo2$foo3);
?>
7

// destroy a single variable
unset($foo);// destroy a single element of an array
unset($bar['quux']);// destroy more than one variable
unset($foo1$foo2$foo3);
?>
8

// destroy a single variable
unset($foo);// destroy a single element of an array
unset($bar['quux']);// destroy more than one variable
unset($foo1$foo2$foo3);
?>
9


00


01


02


03


04


05


06


07


08


09

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
0

12 năm trước

Tecdoc tại Ukr Dot Net ¶


11


12

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
0

Ẩn danh ¶

12 năm trước


14


15

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
0

Tecdoc tại Ukr Dot Net ¶

18 năm trước


17


18


19

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
0

5 tháng trước

12 năm trước


21


22


23


24

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
0

Tecdoc tại Ukr Dot Net ¶

5 tháng trước


26


27


28


29

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
0

Ẩn danh ¶

15 năm trước


31


32

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
0

Lion_Cat tại Mail Ru ¶

Tecdoc tại Ukr Dot Net ¶


34


35


36


37


38

5 tháng trước

12 năm trước


39


40


41


42


43


44


45


46


47


48


49


50


51

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
0

Tecdoc tại Ukr Dot Net ¶

13 năm trước


53


54

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
4


56

Dan at--nospam-- cubeland dot co dot uk ¶

15 năm trước


57

Lion_Cat tại Mail Ru ¶

13 năm trước


58


59

var0


61

Dan at--nospam-- cubeland dot co dot uk ¶

17 năm trước


62


63


64

Phpmanual tại Kennel17 Dot Co Dot Uk ¶

12 năm trước


65


66


67


68

var0


70

Tecdoc tại Ukr Dot Net ¶

5 tháng trước


71


72


73

Before unset: 1, after unset: 23
Before unset: 2, after unset: 23
Before unset: 3, after unset: 23
0

Làm thế nào để bạn xóa một chức năng trong PHP?

Hàm RESET () di chuyển con trỏ bên trong sang phần tử đầu tiên của mảng.Các phương thức liên quan: Dòng điện () - Trả về giá trị của phần tử hiện tại trong một mảng.end () - di chuyển con trỏ bên trong và đầu ra, phần tử cuối cùng trong mảng.. Related methods: current() - returns the value of the current element in an array. end() - moves the internal pointer to, and outputs, the last element in the array.

Unset () trong PHP là gì?

unset () phá hủy các biến được chỉ định.Hành vi của unset () bên trong một hàm có thể thay đổi tùy thuộc vào loại biến bạn đang cố gắng phá hủy.Nếu một biến toàn cầu hóa không được đặt () bên trong một hàm, chỉ có biến cục bộ bị phá hủy.destroys the specified variables. The behavior of unset() inside of a function can vary depending on what type of variable you are attempting to destroy. If a globalized variable is unset() inside of a function, only the local variable is destroyed.

Làm thế nào để bạn hủy bỏ nhiều giá trị trong PHP?

Bạn phải sử dụng cho vòng lặp cho việc này.Bạn có thể sử dụng Foreach Loop nhưng nó sẽ không được đặt tất cả các biến vẫn còn một biến vẫn còn.use for loop for this. you can use foreach loop but it will not unset all variable one variable still remains.

Sự khác biệt giữa unset () và thiếu liên kết () là gì?

Hàm hủy liên kết () được sử dụng khi bạn muốn xóa hoàn toàn các tệp. Hàm unset () được sử dụng khi bạn muốn làm cho tệp đó trống. The unset() Function is used when you want to make that file empty.