PHP/Data Type/unset
Содержание
Deleting an object
class car {
function __destruct() {
// head to car dealer
}
}
$car = new car;
unset($car);
Removing Elements
<?php
$dogs = array("A" => "AA", "Bud" => "BB","C" => "CC");
printf("<pre>%s</pre>\n", var_export($dogs, TRUE));
unset($dogs["C"]);
printf("<pre>%s</pre>\n", var_export($dogs, TRUE));
?>
Removing Elements from Arrays
<?php
$dogs = array("A" => "C", "B" => "D","X" => "Y", "Q" => "T");
printf("%s,", var_export($dogs, TRUE));
unset($dogs["X"]);
printf("%s,", var_export($dogs, TRUE));
?>
Removing Session Data
<?
$_SESSION["foo"] = "bar";
print $_SESSION["foo"];
unset($_SESSION["foo"]);
?>
Using the unset() Function
<?php
$myvar = "This is a string";
unset($myvar); // Destroy the variable
?>
void unset ( mixed var [, mixed var [, mixed ...]] )
The unset( ) function deletes a variable so that isset( ) will return false. Once deleted, you can recreate a variable later on in a script.
<?
$name = "Paul";
if (isset($name)) print "Name is set\n";
unset($name);
if (isset($name)) print "Name is still set\n";
?>
That would print out "Name is set", but not "Name is still set", because calling unset( ) has deleted the $name variable.