Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

null with PHP < and > operators

Can somebody explain how null is mapped in these statements?

null>0; //=> bool(false) null<0; //=> bool(false) null==0; //=> bool(true) 

but

null<-1; // => bool(true) 

I assume it's some mapping problem, but can't crack it.

Tried with PHP 5.3.5-1 with Suhosin-Patch.

like image 245
Szymon Lukaszczyk Avatar asked Apr 11 '11 15:04

Szymon Lukaszczyk


People also ask

What is use of null coalesce operator?

Uses of Null Coalescing Operator: It is used to replace the ternary operator in conjunction with the PHP isset() function. It can be used to write shorter expressions. It reduces the complexity of the program. It does not throw any error even if the first operand does not exist.

How do you null in PHP?

In PHP, a variable with no value is said to be of null data type. Such a variable has a value defined as NULL. A variable can be explicitly assigned NULL or its value been set to null by using unset() function.

Which of the following operator is known as null coalescing in PHP?

Null coalescing operator ¶ The null coalescing operator ( ?? ) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not null ; otherwise it returns its second operand. // if it does not exist.

Is PHP null safe?

Null-safe operator is a new syntax in PHP 8.0, that provides optional chaining feature to PHP. The null-safe operator allows reading the value of property and method return value chaining, where the null-safe operator short-circuits the retrieval if the value is null , without causing any errors.


1 Answers

I would point you to a few pages: http://php.net/manual/en/types.comparisons.php http://php.net/manual/en/language.operators.comparison.php http://php.net/manual/en/language.types.boolean.php

So in your final example:

null<-1 => bool(true) 

The null is cast to false and the -1 is cast to true, false is less than true

In your first two examples null is cast to false and 0 is cast to false, false is not less than or greater than false but is equal to it.

Ohh the fun of null ! :D

like image 175
mark-cs Avatar answered Sep 29 '22 13:09

mark-cs