Get the App
SLTechnology News&Howtos  ›  Development  › 

What are the two methods of destroying variables by php

Shulou Source: shulou.com Published: 2022-06-03 10:04:26 09月20日 Update

Php destroy variables in two ways is what, for this problem, this article describes the corresponding analysis and solution in detail, hoping to help more want to solve this problem of small partners to find a simpler and easier way.

There are two ways to destroy variables in php: 1. use the unset() function, syntax "unset($varname)";2. assign the specified variable "NULL", syntax "$varname=null;".

Operating environment of this tutorial: Windows 7 system, PHP 7.1 version, DELL G3 computer

PHP variable or object destruction can be divided into explicit and implicit destruction:

Explicit destruction, when the object is not referenced will be destroyed, so we can unset or assign NULL to it;

2, implicit destruction, PHP is a scripting language, in the code execution of the last line, all the application of memory to be released.

Therefore, there are two ways to destroy variables:

unset()

$varname=null

For example:

class Human { public $name = ''; public $gender = null; public function __destruct() { echo 'dead! '; } } $a = new Human(); $b = $c = $d = $a;unset($a);$d=null;echo '';var_dump($a);echo '';var_dump($b);echo '';var_dump($c);echo '';var_dump($d);

The results were as follows:

Notice: Undefined variable: a in/Library/WebServer/Documents/test.php on line 42NULLobject(Human)#1 (2) { ["name"]=> string(6) "Zhang San" ["gender"]=> NULL }object(Human)#1 (2) { ["name"]=> string(6) "Zhang San" ["gender"]=> NULL }NULL Dead!

First of all, we need to know that in PHP, variable names are stored in the memory stack. It is an address that points to specific memory in the heap. We can find memory in the heap by variable names; therefore, we can conclude:

Tags: Variables memory methods objects results questions Zhang San pointing more syntax help solutions easy easy one line for example between code content functions Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Docker NVidia Xiaomi MySQL