Dima Dz Dima Dz - 1 month ago 4x
PHP Question

PHP if statement null === $variable: what for?

Often, I see in if statements for php something like this:

if (null === $variable) {
// do stuff

What does it do and what is it for?

EDIT: I totally get that it is a comparison operator, I just wonder why not
$variable === null


It's not an assignment, it's a comparison for equality. It determines if the variable $variable contains the value null.

More in the documentation:

why not to check $variable === null

Some people like to use the form with the constant on the left (a "Yoda condition", it is called) so that if they have a typo and only type a single =, it causes a syntax error rather than doing an assignment.