Null vs. False vs. 0 in PHP
It's language specific, but in PHP :
Null
means "nothing". The var has not been initialized.
False
means "not true in a boolean context". Used to explicitly show you are dealing with logical issues.
0
is an int
. Nothing to do with the rest above, used for mathematics.
Now, what is tricky, it's that in dynamic languages like PHP, all of them have a value in a boolean context, which (in PHP) is False
.
If you test it with ==
, it's testing the boolean value, so you will get equality. If you test it with ===
, it will test the type, and you will get inequality.
So why are they useful ?
Well, look at the strrpos()
function. It returns False if it did not found anything, but 0 if it has found something at the beginning of the string !
<?php
// pitfall :
if (strrpos("Hello World", "Hello")) {
// never exectuted
}
// smart move :
if (strrpos("Hello World", "Hello") !== False) {
// that works !
}
?>
And of course, if you deal with states:
You want to make a difference between DebugMode = False
(set to off), DebugMode = True
(set to on) and DebugMode = Null
(not set at all, will lead to hard debugging ;-)).
Difference between NULL, 0 ,False and ''?
Well, after massive comments and then poof deletion of them... may as well try to help you out.
null
is the absence of value.
0
can be a numeric value, or a representation of a boolean FALSE
, or a string. PHP doesn't really have variable typing, so depending on what you are checking for using ==0
or ===0
or ==false
or ===false
may be appropriate.
You may want to read over this - https://www.virendrachandak.com/techtalk/php-isset-vs-empty-vs-is_null/ as well as the PHP manual for isset()
and empty()
Why '0' does not equal null
A 'real' (strict) equivalence in PHP is ===
. If you use it you do enjoy the transitivity property you've mentioned.
But ==
isn't an exact equivalence in PHP. It employs converting operands to a common type first. As we can see, using false
as the first operand causes the second one to be reduced toward a Boolean value. If, however, you compare a reference to a string, no such reduction happens that's why the values are treated to be different. Of course such an approach violates the transitivity property. But, again, ===
is a 'real' equality sign rather than ==
.
How to differentiate a value is zero or NULL in php?
to check for null use :
if (is_null($moon))
to check for integer 0 use:
if ($moon === 0)
to check for integer 0 or string "0"
if ($moon == '0')
I have to add that php will consider any string equals with number 0 when using "==":
$moon = "asd";
if ($moon == 0) // will be true
How to differentiate between null or and 0 in PHP?
Use the ===
and !==
operators for these sort of comparisons. These "strict" or "true" comparison. These operators were invented specifically to help deal with the sort of comparison problems you've described.
You also might be interested in the PHP comparison tables. This docs page runs through a lot of the various edge cases in PHP equality.
In PHP, what is the differences between NULL and setting a string to equal 2 single quotes
Null is just another datatype in PHP, which has only one value (null). Since PHP is a loosly typed language, it can be confusing how it handles different values.
"", 0, "0", False, array(), Null
are all considered False in PHP.
Null, however, is a different kind of animal. The main incompatibility with using Null is that you cannot tell if it isset().
$x = false;
isset($x) -> true
echo $x -> ""
$y = null;
isset($y) -> false
echo $y -> ""
//$z is not set
isset($z) -> false
echo $z -> E_NOTICE
So null is odd in the sense that it doesn't follow normal variable rules in PHP (at least some). In most cases, it is fine.
When it comes to database columns, PHP's NULL has no place there. You see, SQL is a string based language. SQL's NULL must be represented by NULL
with no quotes.
So if you want an EMPTY field, set it to ""
INSERT INTO foo SET bar = ""
But if you want a NULL field, set it to NULL
INSERT INTO foo SET bar = NULL
BIG DIFFERENCE.
But if you try to insert the PHP NULL directly, it will add zero characters to the query, (which leaves you with a blank or syntax error, depending on if you quoted it).
PHP considers null is equal to zero
$myvariable === 0
read more about comparison operators.
php int comparison smaller or equal than returns empty or null?
This is due to some strange (although documented) default behaviour in PHP when converting between data types:
Boolean FALSE is converted to "" (the empty string).
You can change your code to e.g. this to see a difference:
echo ('dfsdfds: '.(int)$item[0] . ' ' .
(int)$box[0] . ' ' .
( ( (int)$item[0] <= (int)$box[0] )?'true':'false') );
Or if you want to have 0
or 1
respectively, you can force a cast to int
:
echo ('dfsdfds: '.(int)$item[0] . ' ' .
(int)$box[0] . ' ' .
( ( (int)$item[0] <= (int)$box[0] )?true:(int)false) );
(true
is already automatically converted to "1"
)
Is it ok to use a 'boolean operator' to evaluate null in PHP
I remembered what I was looking for:
if (null === $b = a(false)) { ... }
It's a strange but elegant construct. Sorry if the intent wasn't clear from the question.
Related Topics
What Does PHP Keyword 'Var' Do
How to Add Http:// If It Doesn't Exist in the Url
List All the Files and Folders in a Directory With PHP Recursive Function
Laravel 5.5 Ajax Call 419 (Unknown Status)
How to Do This in Laravel, Subquery Where In
A Non Well Formed Numeric Value Encountered
Where Does PHP'S Error Log Reside in Xampp
Does PHP Allow Named Parameters So That Optional Arguments Can Be Omitted from Function Calls
Ajax and PHP to Enter Multiple Forms Input to Database
Insert Blobs in MySQL Databases With PHP
Getting Title and Meta Tags from External Website
Http Authentication Logout Via PHP
Make Xampp/Apache Serve File Outside of Htdocs Folder
How to Benchmark Efficiency of PHP Script
How to Check Uploaded File Type in PHP
How to Add/Remove Pkcs7 Padding from an Aes Encrypted String