can anybody tell me the difference of using '===' as
if (null === $this->getName())
and
if ($this->getName() === null )
if $this->getName is already defined.
thanks in advance
There is no difference between the two conditionals, however, it is a common practice to place the value you are checking first, so you don't accidentally turn a conditional check into an assignment operation:
// Conditional
if ($this->getName() === null )
// Assignment
if ($myName = null )
// Avoids the confusion of either the above
if (null === $this->getName())
You can also use the is_null PHP function for testing if variables are null.
There is no difference as long as you are using ===
or ==
.
Now you might wonder why someone written all their values at left side. This is because we devs tend to forget or have typo writing ==
and we type =
. This is makes accidental assignment.
if($id=13){
echo "foo";
}
Its hard to find the problem in above code. Which should have been written as $id==13
. Those who writes as 13==$id
doesn't have to deal with the issue becase when they forget a =
it becomes 13=$id
which is a syntax error. This way such almost impossible to detect errors are avoided.
The ===
operator in PHP is symmetric, so those are identical.
From what I've read, the only notable difference is strictly in the order of writing, enabling an easier following of given tests in some cases.
I also consider PHP evaluating first term first, but this won't be producing any difference since they will both be evaluated anyway.