PHP/Data Type/unset

Материал из Web эксперт
Версия от 07:03, 26 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

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.