PHP: order of operators in a comparison null

operators, php

Solution

If you accidentally forget one of the `=`, the second one will give an error.

if ($myvar = null)

This will assign `null` to `$myvar` and perform the `if` check on the result.

if (null = $myvar)

This will try to assign the value of `$myvar` to `null` and give an error because you cannot assign to `null`.

Problem

I usually write this: ``` if ($myvar == null) ``` but sometimes I read this: ``` if (null == $myvar) ``` I remember somebody told me the latter is better but I don't remember why. Do you know which is better and why? Thanks, Dan

Original source

Related problems