Jump to content

Can Someone Clarify The Meaning Of Null


skaterdav85
 Share

Recommended Posts

It depends on the programming language. In PHP, NULL has its own special meaning:

The special NULL value represents a variable with no value...A variable is considered to be null if: * it has been assigned the constant NULL. * it has not been set to any value yet. * it has been unset().
In PHP, null, along with empty strings, 0, and some other things, are all considered false. This is, however, not true in some other languages, such as Java, where false is again a distinct value.
When converting to boolean, the following values are considered FALSE: * the boolean FALSE itself * the integer 0 (zero) * the float 0.0 (zero) * the empty string, and the string "0" * an array with zero elements * an object with zero member variables (PHP 4 only) * the special type NULL (including unset variables) * SimpleXML objects created from empty tags
Link to comment
Share on other sites

so would these be equivalent?
unset($var)$var=NULL$var=""

No, those are two different things.One indicates that a variable has no value, the other indicates that a variable contains a string with length zero.
Link to comment
Share on other sites

Note that null only evaluates to false if you use loose comparison. If you use strict comparison null only equals null, and false only equals false.

$null = null;$false = false;$str = '';if ($null == $false){  echo 'Null and false are considered equivalent for loose comparison. ';}if ($false == $str){  echo 'The empty string is considered equivalent to false for loose comparison. ';}if ($null === $false){  echo 'This will not print.';}if ($null !== $false){  echo 'Null is not equivalent to false for strict comparison. ';}if ($str !== $false){  echo 'The empty string is not equivalent to false for strict comparison. ';}

Link to comment
Share on other sites

whats the difference between loose and strict comparison?
A loose comparison evaluates only the values of each side of a condition, whereas a strict comparison checks value AND type.An empty string: ""and zero integer: 0are considered to have the same value.BUT an empty string and a zero integer do not have the same type.
Link to comment
Share on other sites

for php his means the following (source php.net)

<?php//This condition will return falseif(isset($var)){ echo $var.' is set and can be empty or not';}$var = 0;// Evaluates to true because $var is emptyif (empty($var)) { echo '$var is either 0, empty, or not set at all';}// Evaluates as true because $var is setif (isset($var)) { echo '$var is set even though it is empty';}$var = "11";// Evaluates to false because $var is filledif (empty($var)) { echo '$var is either 0, empty, or not set at all';}//Delete the $varunset($var);//Now the $var is deleted so it is not empty because it does NOT even exist?>
You can compare the variables in php with a container. They are there to hold something (integers, strings etc) in securitychecks (in php if/else statements) the first scanner scans IF the container is present (isset... IS SET) and the second scanner scans if there is something in the container (=EMPTY...TRUE or not... FALSE). Edited by Redroest
Link to comment
Share on other sites

Really? so what about 0 and "0"...Does this mean that "" will equal "0" in loose comparison?
When converted to boolean, yes."" is equivalent to false and"0" is equivalent to false
$test1 = "";if ($test1 == true) {echo "true";} else {echo "false";} //echoes false$test2 = "0";if ($test2 == true) {echo "true";} else {echo "false";} //also echoes falseif ($test1 == $test2) {echo "true";} else {echo "false";} //This will also echo false

When compared to a boolean $test1 and $test2 are evaluated the same, but when compared to each other, since they are both strings their string values are compared.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...