Dima Dz Dima Dz - 1 year ago 93
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

Answer Source

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.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download