PHP/Data Type/unset — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
Admin (обсуждение | вклад) м (1 версия) |
(нет различий)
|
Текущая версия на 07:03, 26 мая 2010
Содержание
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.