PHP/Data Type/unset

Материал из Web эксперт
Перейти к: навигация, поиск

Deleting an object

   <source lang="html4strict">

class car {

   function __destruct() {
       // head to car dealer
   }

} $car = new car; unset($car);

 </source>
   
  


Removing Elements

   <source lang="html4strict">

<?php


 $dogs = array("A" => "AA", "Bud" => "BB","C" => "CC");
printf("
%s
\n", var_export($dogs, TRUE));
 unset($dogs["C"]);
printf("
%s
\n", var_export($dogs, TRUE));

?>

 </source>
   
  


Removing Elements from Arrays

   <source lang="html4strict">

<?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)); ?>

 </source>
   
  


Removing Session Data

   <source lang="html4strict">

<?

   $_SESSION["foo"] = "bar";
   print $_SESSION["foo"];
   unset($_SESSION["foo"]);

?>

 </source>
   
  


Using the unset() Function

   <source lang="html4strict">

<?php

   $myvar = "This is a string";
   unset($myvar);        // Destroy the variable

?>

 </source>
   
  


void unset ( mixed var [, mixed var [, mixed ...]] )

   <source lang="html4strict">

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.

 </source>