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!

msanford
  • 11,803
  • 11
  • 66
  • 93
  • 1
    When performance considerations become a real concern, try also profiling. That one won't matter in comparison to the function call. – mario Jan 16 '12 at 23:39
  • @mario Yup. I'm simply curious about the comparison operator as the notion came to mind while I was writing a function (questioning my habits.) – msanford Jan 17 '12 at 14:17

2 Answers2

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.

Michael Fenwick
  • 2,374
  • 2
  • 19
  • 28
  • 1
    +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. – zrvan Jan 16 '12 at 22:54
  • 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. – Michael Fenwick Jan 16 '12 at 22:56
  • Performance aside, what about `isset($json)` rather than `!is_null($json)`? And avoid a negative expression. – MrWhite Feb 01 '12 at 14:42
  • `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. – Michael Fenwick Feb 02 '12 at 00:28
-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

miqbal
  • 2,213
  • 3
  • 27
  • 35