note 12161 deleted from language.types by jeroen
| From: | jeroen@php.net | Date: | Sun, 28 Oct 2001 18:49:32 +0000 |
| Subject: | note 12161 deleted from language.types by jeroen | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-19532@lists.php.net to get a copy of this message | ||
Here is a program to test the differences between NULL, unset and false. First the result of the
program:
-----------------------------------
PART I: Check difference between NULL and false:
(A=NULL, B=false, UNSET VAR is made unset() )
TYPE of A is NULL
TYPE of B is boolean
TYPE of UNSET VAR is NULL
EQUALITY:
A==B
A!==B (type unequivalence, A type NULL, B type BOOL)
A == UNSET VAR
A === UNSET VAR
B == UNSET VAR
B !== UNSET VAR
UNSET():
A is unset
B is set
UNSET VAR is unset
IS_NULL():
A is NULL
B is NOT NULL
UNSET VAR is NULL
The result of these tests are, that the type NULL is completly equvalent to an UNSET VALUE.
PART II: Ok, now check difference between unset() and NULL
A WARNING should now appear, cause we want to 'echo $unset_var' :
Warning: Undefined variable: unset_var in /var/www/dev0.fidion.de/htdocs/ssilk/test/unset1.php on
line 69
^^^^^ This warning is correct, cause error_reporting = E_ALL
For equvalent behavior a WARNING should also appear, if we want to 'echo $a':
But there is no warning, although $a is unset (see above)
The result of this test is, that NULL is handled by the interpreter in another way, than an UNSET
VALUE.
This is in discrepancy to PART I!
PART III: Now show, that empty() is not equal zu strlen():
Expression 'empty($unset_var)' returns: 1
Warning: Undefined variable: unset_var in /var/www/dev0.fidion.de/htdocs/ssilk/test/unset1.php on
line 90
Expression '!strlen($unset_var)' returns: 1
This causes an error, because $a is unset.
The result of this test is, that it is in PHP4 obsolete to use strlen() instead of empty(). For
propper programming it is forbidden!
--------------------------------
The results can currently seen on http://dev0.fidion.de/ssilk/test/
<?php
echo '<h2>PART I: Check difference between NULL and false:</h2>
(A=NULL, B=false, UNSET VAR is made unset() )<br><br>';
$a=NULL;
$b=false;
unset($unset_var);
echo "TYPE of A is ".gettype($a)."<br>\n";
echo "TYPE of B is ".gettype($b)."<br>\n";
echo "TYPE of UNSET VAR is ".gettype(@$unset_var)."<br>\n";
echo "<br>EQUALITY:<br>";
if ($a == $b) echo "A==B<br>\n";
else echo "A!=B<br>\n";
if ($a === $b) echo "A===B (type equivalence)<br>\n";
else echo "A!==B (type unequivalence, A type NULL, B type BOOL)<br>\n";
if ($a == @$unset_var) echo "A == UNSET VAR<br>\n";
else echo "A != UNSET VAR<br>\n";
if ($a === @$unset_var) echo "A === UNSET VAR<br>\n";
else echo "A !== UNSET VAR<br>\n";
if ($b == @$unset_var) echo "B == UNSET VAR<br>\n";
else echo "B != UNSET VAR<br>\n";
if ($b === @$unset_var) echo "B === UNSET VAR<br>\n";
else echo "B !== UNSET VAR<br>\n";
echo "<br>UNSET():<br>";
if (!isset($a)) echo "A is unset<br>\n";
else echo "A is set<br>\n";
if (!isset($b)) echo "B is unset<br>\n";
else echo "B is set<br>\n";
if (!isset($unset_var)) echo "UNSET VAR is unset<br>\n";
else echo "UNSET VAR is set<br>\n";
echo "<br>IS_NULL(): <br>";
if (is_null($a)) echo "A is NULL<br>\n";
else echo "A is NOT NULL<br>\n";
if (is_null($b)) echo "B is NULL<br>\n";
else echo "B is NOT NULL<br>\n";
if (is_null(@$unset_var)) echo "UNSET VAR is NULL<br>\n";
else echo "UNSET VAR is NOT NULL<br>\n";
?>
<br><br>
The result of these tests are, that the type NULL is completly equvalent to
an UNSET VALUE.
<?php
##########################################################################
echo "<hr><h2>PART II: Ok, now check difference between unset() and
NULL</h2>";
echo "A WARNING should now appear, cause we want
to 'echo \$unset_var' :<br>\n";
echo $unset_var;
echo "^^^^^ This warning is correct, cause error_reporting = E_ALL<br>\n";
echo "<br>For equvalent behavior a WARNING should also appear, if we want to
'echo \$a':<br>\n";
echo $a;
echo "But there is no warning, although \$a is unset (see above)<br>\n";
?>
<br><br>
The result of this test is, that NULL is handled by the interpreter in
another way, than an UNSET VALUE.
<br>
This is in discrepancy to PART I!
<?php
###########################################################################
echo "<hr><h2>PART III: Now show, that empty() is not equal zu
strlen():</h2>";
echo "Expression 'empty(\$unset_var)' returns: ".(empty($unset_var))
."<br>\n";
echo "Expression '!strlen(\$unset_var)' returns:
".(!strlen($unset_var))."<br>\n";
echo "This causes an error, because \$a is unset.<br>\n";
?>
<br><br>
The result of this test is, that it is in PHP4 obsolete to use strlen() instead of empty(). For
propper programming it is forbidden!
<hr>
<a href="<?php echo $HTTP_SERVER_VARS['PHP_SELF']
?>s">Sourcode</a>
<br><br><br><br><br><br>
<?php
phpinfo();
?>