Hello, everyone! Today, I want to know when to use the unset () function in PHP? Everyone knows that unset is used to cancel the assignment. For example, $ aa & quot; asdfasdf & quot; echo $ aa; the result is asdfasdfunset $ aa; echo $ aa; the result is null... hello, everyone!
Today, I want to know when to use the unset () function in PHP?
Everyone knows that unset is used to cancel the assignment, for example
$ Aa = "asdfasdf ";
Echo $ aa; // The result is asdfasdf.
Unset $ aa;
Echo $ aa; // The result is null.
It is used to know
What I wonder about is that in which cases should I use the unset () function?
What are the benefits or disadvantages of using the unset () function?
Thank you, Tom. o (zookeeper) o... haha, ^ _ ^
Reply content:
Hello, everyone!
Today, I want to know when to use the unset () function in PHP?
Everyone knows that unset is used to cancel the assignment, for example
$ Aa = "asdfasdf ";
Echo $ aa; // The result is asdfasdf.
Unset $ aa;
Echo $ aa; // The result is null.
It is used to know
What I wonder about is that in which cases should I use the unset () function?
What are the benefits or disadvantages of using the unset () function?
Thank you, Tom. o (zookeeper) o... haha, ^ _ ^
Unset is used to cancel a variable and release the name. The application scenario is not too much. Because PHP does not need to release resources, the PHP process will only exist for a short time.
I think the application on the array should be a little more used to delete an element in the array. For example, to insert a user's information into the database, I defined an array like this:
$user = ["name" => "xxoo", "passwd" => "4d5c01842f37d", "email" => "xxoo@xo.ox"];$db->insert($user);
Then I want to insert the data into the log, but I do not want to save the password in the log. In this case, I only need:
unset($user["passwd"]);$log->append($user);
Because of php's Automatic Resource recovery mechanism, when the page is executed, the occupied memory will be automatically released. Therefore, it is generally not used to destroy the variable to release the memory.
However, when a large array and other variables are large, you can unset them to release the memory. Especially when the available memory is relatively tight.
Note that,
Unset can only destroy local variables in the function. What should I do if I need to destroy global variables in the program? It is also very easy to implement with the $ GLOBALS array.