I was wondering, in PHP land, what is more performant? Strict Not Equals ( $var !== null) or Not Strict Equals ( !($var === null)), so I did some digging, in case anyone else was wondering the same.
Turns out, there is a difference, and frankly, if you have gotten this far, you have already wasted more time than computational time you’ll save knowing this, and as a result of writing this, I have wasted more time than all of you would save using this information. That said, here is the general breakdown (Note – The ERROR flag is raised because the variable is not set, expected):
For $not_null = $var !== null
:
1 2 3 4 5 6 7 8 9 |
Finding entry points Branch analysis from position: 0 Jump found. Position 1 = -2 number of ops: 2 compiled vars: !0 = $not_null, !1 = $var line #* E I O op fetch ext return operands ------------------------------------------------------------------------------------- 1 0 E > IS_NOT_IDENTICAL ~2 !1, null 1 ASSIGN !0, ~2 |
For $not_null = !($var === null)
:
1 2 3 4 5 6 7 8 9 10 |
Finding entry points Branch analysis from position: 0 Jump found. Position 1 = -2 number of ops: 3 compiled vars: !0 = $not_null, !1 = $var line #* E I O op fetch ext return operands ------------------------------------------------------------------------------------- 1 0 E > IS_IDENTICAL ~2 !1, null 1 BOOL_NOT ~3 ~2 2 ASSIGN !0, ~3 |
And if this is in a conditional, you can ignore the ASSIGN, which means $var !== null is HALF the opcodes of !($var === null)! FANCY!
Now, while this is good/interesting to know, and not necessarily a bad thing to keep in mind when programming, you probably would have saved more time reading something else. Oh well.
Cheers!