PHP unset

 PHP unset is used to remove the value of a variable . After clearing, it will not return any results. In versions after 4.0.1, support for multiple parameters has been added. Simply use unset and just pass the variable to the unset function. , But it should be noted that if the variable you want to remove is the global variable in the function, then only the partial variable is removed.


PHP unset syntax example

void unset (variable to be removed)


PHP unset implementation example
<?php
$i=1;
echo'Original $i ='.$i.';';
unset($i);
echo'$i after executing unset ='.$i; // Output result: original $i = 1; $i =
?> after executing unset
The results can be seen by the output has been unset variables original value cleared, so the right side of the equal sign the final output of nothing, because the results will not unset any message in return to you, just simply remove the variable value only , Let’s look at the result of removing the variable in a function and calling the variable value of the function repeatedly.

unset result in function
<?php
function TestUnset() {
 static $i;
 $i=++; echo'before
 execution $i ='.$i.';';
 unset($i);
 $i = 55;
 echo'after execution$ i ='.$i.'<br>';
}

TestUnset(); // Output result: before execution $i = 1; after execution $i = 55
TestUnset(); // Output result: before execution $i = 2; after execution $i = 55
?>
In the example, the function executes two parts of the action. They are first to output the original value of $i, then use unset to clear the value of $i and output the value of $i again. We call the function twice and find the $i before it is executed. It will increment by 1, but the result after execution is the same. It means that after the unset is executed for the first time, the variable $i has been incremented, but unset clears its value and re-assigns the value of 55, but when the function is called the second time When, the variable $i will be restored to the value before the previous unset.

For related instructions on global variables, please see the instructions on the official PHP website: PHP: unset-Manual .

Post a Comment

0 Comments