note 11973 added to function.unset.php

From: Date: Thu, 15 Mar 2001 23:29:42 +0000
Subject: note 11973 added to function.unset.php
Groups: php.notes 
Request: Send a blank email to php-notes+get-12451@lists.php.net to get a copy of this message
Semi-equivalence between unset() and NULL.<br>There are some similarities and differences between using unset() and assigning NULL.<br> SIMILARITIES:<br> $myvar1 = 'Alpha'; unset($myvar1); $myvar2 = 'Bravo'; $myvar2 = NULL; // never defined $myvar3 Now all three -- $myvar1, $myvar2, and $myvar3 -- give identical results when tested with isset (FALSE in each case), empty (TRUE), var_dump (NULL), echo (nothing) and gettype (NULL). DIFFERENCES:<br> $myarray1 = array(); $myarray1['myelem1'] = 'Charlie'; // optional unset($myarray1['myelem1']); $myarray2 = array(); $myarray2['myelem2'] = 'Delta'; // optional $myarray2['myelem2'] = NULL; $myarray3 = array(); // never defined $myarray3['myelem3'] Now, the three array _elements_ $myarray1['myelem1'], $myarray2['myelem2'], $myarray3['myelem3'] give identical results as before. But the _arrays_ themselves give different results when tested with sizeof (arrays 1 and 3 give count/sizeof as 0 elements, array 2 gives count/sizeof as 1 element) and var_dump. Arrays 1 and 3 show 0 elements: array(0) { } whereas array 2 shows 1 element: array(1) { ["myelem2"]=> NULL } (I tested this on PHP 4.0.3pl1. is_null is not available in this version.) http://www.php.net/manual/en/function.unset.php

« previous php.notes (#12451) next »