Should one use `if ($a != NULL)` or `if ($a !== NULL)` to control program flow?
Asked Answered
H

2

6

This is perhaps a painfully basic question to answer, but I'm wondering about performance issues regarding using PHP's if identical !== versus if equal != to control flow.

Consider the following trivial PHP function:

<?php
 function test_json($json = NULL) {
  if ($json != NULL) {
   echo 'You passed some JSON.';
  } else {
   echo 'You failed to pass any JSON.';
  }
 }
?>

From a performance standpoint, is it preferable to employ if identical (!==) to prevent PHP iterating through variable types, attempting to find a valid comparison?

I assume that !== first compares the variable types, and if that fails, it immediately returns FALSE? I've used != since PHP3 almost as a reflex. Now that I'm working on some much more computationally-intensive projects, minute performance considerations become more of a concern.

Other comments on flow control optimization are, of course, welcome!

Hogfish answered 16/1, 2012 at 22:37 Comment(2)
When performance considerations become a real concern, try also profiling. That one won't matter in comparison to the function call.Socialite
@Socialite Yup. I'm simply curious about the comparison operator as the notion came to mind while I was writing a function (questioning my habits.)Hogfish
D
9

I haven't done any performance tests on loose vs strict comparison operators, but for what you are trying to do, I would instead recommend something like

if (!is_null($json)) {
    do_stuff()
}

More information on is_null() at http://www.php.net/manual/en/function.is-null.php

EDIT: a note in the comments of the php page I linked to above has some results showing that the === operator is slightly faster than the == operator, both of which are faster than is_null(). However, another note points out that "The execution time difference between ===NULL and is_null is less than 250 nanoseconds. Go optimize something that matters." I'd have to agree there. So all that said, I would suggest you go with what you deem to be the most readable.

Disorganize answered 16/1, 2012 at 22:41 Comment(4)
+1, Running a test with 1 000 000 comparisons each in a loop comparing a value to NULL; ==, === and is_null(), I get that == and === are roughly the same === being slightly faster and is_null() taking about twice the time. However, as a matter of personal taste, I prefer is_null() non the less, NULL == NULL should be false (but isn't) and is_null() captures that notion.Perchloride
Excellent point about NULL == NULL. I tend to err on the side of functions included in a language where possible for this exact reason: they are likely to properly handle weird corner cases I never would have thought of, meaning less debugging later.Disorganize
Performance aside, what about isset($json) rather than !is_null($json)? And avoid a negative expression.Tolle
isset() and is_null are logical opposites, so either will work fine, though I'm not sure how they compare performance-wise. At that point I'd say it's mostly just style and what is clearest in the given context as to which is better to use.Disorganize
A
-1

You could write a test code like this before asking; according to test "Using "===" is 30x quicker than is_null()."

http://www.php.net/manual/en/language.types.null.php#77937

Aragonite answered 16/1, 2012 at 22:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.