How to use function to destroy variables in php

醉折花枝作酒筹
Release: 2023-03-11 22:18:02
Original
1707 people have browsed it

In the previous article, we learned about the method of deserializing objects or arrays. If you need it, please read "How to Deserialize Arrays and Objects in PHP". This time we will introduce to you how to use functions to destroy variables. You can refer to it if you need it.

Before starting the article, let’s first take a look at what is the destruction of variables.

The destruction of PHP variables or objects can be divided into explicit destruction and implicit destruction:

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

2. Implicit destruction. PHP is a scripting language. When the last line of code is executed, all applied memory must be released.

What we are going to introduce today is unset in explicit destruction.

Let’s look at a little chestnut first.

<?php
class Human { 
  public $name = &#39;张三&#39;; 
  public $gender = null; 
  public function __destruct() { 
      echo &#39;结束!<br />&#39;; 
  } 
} 
$a = new Human(); 
$b = $c = $d = $a;
unset($a);
$d=null;


echo &#39;<hr />&#39;;
var_dump($a);
echo &#39;<hr />&#39;;
var_dump($b);
echo &#39;<hr />&#39;;
var_dump($c);
echo &#39;<hr />&#39;;
var_dump($d);
?>
Copy after login

The result is

How to use function to destroy variables in php

Without further ado, let’s take a look at this function.

The unset() function is used to destroy the given variable.

So unset() does not actually destroy the memory value in the variable, it just cuts off the relationship between the variable and the memory, and removes the variable name, but as long as the memory is still referenced, it will not is released; in PHP, the value of objects is passed by reference by default, which also explains that in the Human class, $a is unset(), but $ b =$c = $d also has value.

Let’s take a look at the syntax of this function.

void unset (要销毁的变量)
Copy after login

If unset() is a global variable in a function, only local variables are destroyed, and variables in the calling environment will retain the same value as before calling unset().

If unset() is a variable passed by reference in a function, only the local variable is destroyed, and the variables in the calling environment will retain the same value as before calling unset().

If unset() is a static variable in a function, the static variable will be destroyed in the function. However, when this function is called again, the static variable will be restored to the value it had before it was last destroyed.

That’s all. If you want to know anything else, you can click here. → →php video tutorial

The above is the detailed content of How to use function to destroy variables in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template