PHP considers null is equal to zero
$myvariable === 0
read more about comparison operators. 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
In php, is 0 treated as empty?
http://php.net/empty
Note that this is exactly the same list as for a coercion to BooleanThe following things are considered to be empty:
- "" (an empty string)
- 0 (0 as an integer)
- 0.0 (0 as a float)
- "0" (0 as a string)
- NULL
- FALSE
- array() (an empty array)
- var $var; (a variable declared, but without a value in a class)
false
. empty
is simply !isset($var) || !$var
. Try isset
instead. 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 ;-)).
Zero considered as empty in PHP
you may use
if ($_POST['tmax'] == "") {
$tmax = null;
}else {
$tmax = $_POST['tmax'];
}
How can PHP 5.3 think 0 is null?
Use is_null
function
is_null($test)
Test
$test = 0;
if (is_null($test)) print 'is null';
else print 'not null';
// Return not null
How to distinguish between 0 and null?
is_null
does not consider the value 0
equal to null
.
Your code uses ==
for the comparison, which does not make this distinction. Use the identical operator ===
or is_null
instead:
// One way to do it:
if(!$p12_score1 !== NULL)
// Another:
if(!is_null($p12_score1))
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 NULLINSERT 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).
Related Topics
Laravel Customized Session.Lifetime at User Level
Create an Assoc Array with Equal Keys and Values from a Regular Array
How to Find "Related Items" in PHP
Codeigniter Active Record Not Equal
Sort Array Based on the Datetime in PHP
Why Do PHP Array Examples Leave a Trailing Comma
Facebook Like Custom Profile Url PHP
How to Add Custom Fields to Woocommerce Registration Form
How to Access Outer Local Variable in PHP
PHP Using Regex to Get Substring of a String
Running PHP 5.4 Built-In Web Server Outside Localhost
How to Force a Certain Tls Version in a PHP Stream Context for the Ssl:// Transport
Customizing My-Account Addresses Fields in Woocommerce 3