Use Cases of unset () functions in PHP

Source: Internet
Author: User
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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.