Null?
written by: admin
Date Written: 3/28/11
Last Updated: 4/8/11
NULL is an empty value. What constitutes an empty value is shown below:
<?php
$a="";
if (is_null($a)){echo "NULL<br>";} else echo "NOT NULL<br>";
if (is_null($b)){echo "NULL<br>";} else echo "NOT NULL<br>";
$c=$_GET['c'];
if (is_null($c)){echo "NULL<br>";} else echo "NOT NULL<br>";
$d=$_GET['d'];
if (is_null($d)){echo "NULL<br>";} else echo "NOT NULL<br>";
$e="4";
unset($e);
if (is_null($e)){echo "NULL<br>";} else echo "NOT NULL<br>";
$f="set";
$f=$h;
if (is_null($g)){echo "NULL<br>";} else echo "NOT NULL<br>";
$i=0;
if (is_null($i)){echo "NULL<br>";} else echo "NOT NULL<br>";
?>
NOT NULL
NULL
NOT NULL
NULL
NULL
NULL
NOT NULL
The first is
NOT NULL, because the variable has a value of nothing.
The second is
NULL because no value has been given to it yet.
The third is
NOT NULL because the URI is
http://www.animeviews.com/test.php?c=
The fourth is
NULL because the value for d was not specified in the uri.
The fifth value is
NULL because the value was unset.
The sixth value is also
NULL because the value was assigned the NULL, or empty, value that $h has.
The seventh variable has a
NOT NULL value, because it is set to the value of the integer "0".
Why is this important?
This has become important in determining whether a value in the database exists, especially when the value of the ID is 0.
What is the value of a non existent item from the database?
<?php
$a="0";
$b="";
$c=0;
if ($a==$b) echo"1 same<br>"; else echo"1 not same<br>";
if ($a===$b) echo"2 same<br>"; else echo"2 not same<br>";
if ($a==="") echo"3 same<br>"; else echo"3 not same<br>";
if ($a===$c) echo"4 same<br>"; else echo"4 not same<br>";
if ($b===$c) echo"5 same<br>"; else echo"5 not same<br>";
if ($b==$c) echo"6 same<br>"; else echo"6 not same<br>";
if ($b==0) echo"7 same<br>"; else echo"7 not same<br>";
if ($b==$d) echo"8 same<br>"; else echo"8 not same<br>";
if ($b===$d) echo"9 same<br>"; else echo"9 not same<br>";
?>
This produces:
1 not same
2 not same
3 not same
4 not same
5 not same
6 same
7 same
8 same
9 not same
= assign a value
== compare two values
=== compare values and datatypes.
For example with
$a=="0"; you are saying the variable
$a has the value of the character
0 as opposed to the number
0. To give
$a the value of the number or integer
0 you need to remove the quotes.
see also
http://www.php.net/manual/en/types.comparisons.phpTAGS: php