PHP: empty() vs isset() vs is_null()

by alex, 2014-10-14

I used to use

  1. empty()
empty()
if I want to check whether variable has value. But some time ago I decided to check which of them is faster. Additionally I’ve added to test function
  1. is_null()
is_null()
. Purpose of these functions almost the same.

Test machine:

  • Intel(R) Core(TM) i7-3740QM CPU @ 2.70GHz
  • OSX, Mavericks
  • only 1 tread for server
  • PHP 5.5.17
  • memory limit: 64M

isset() vs empty() vs is_null() performance

Iterations,Millions isset($var),sec empty($var),sec is_null($var),sec
1 0.1578 0.15687 0.81775
2 0.31174 0.3314 1.65696
3 0.47739 0.56433 2.44841
4 0.62813 0.64015 3.27769
5 0.78073 0.80249 4.06912
6 0.92631 0.95377 4.91195
7 1.07459 1.08981 6.02254
8 1.25033 1.2407 6.59814
9 1.38887 1.40384 7.59789
10 1.54892 1.56017 8.34764
Iterations,Millions isset($var),sec empty($var),sec is_null($var),sec

The results were predictable for me, at least according to

  1. isset()
isset()
and
  1. empty()
empty()
. BUT i was surprised a little with
  1. is_null()
is_null()
. I supposed that this function will be slower but not in 8 times.

So, when I want to check

  1. $val
$val
is null, what should I use?

is_null() vs comparison operators

Iterations,Millions is_null($var) $var == null $var === null
1M 0.74273 0.17821 0.16878
2M 1.50407 0.36228 0.33803
3M 2.35153 0.52707 0.50511
4M 3.0704 0.70322 0.67562
5M 3.82585 0.89502 0.84842
6M 4.60788 1.05788 1.0336
7M 5.57395 1.2263 1.19034
8M 6.26093 1.39313 1.34496
9M 7.14926 1.56232 1.51879
10M 7.70948 1.75243 1.69789
Iterations,Millions is_null($var) $var == null $var === null

According to test results,

  1. is_null()
is_null()
the slower than simple comparison operators. Thus, looks like
  1. is_null()
is_null()
makes more operations undercover than comparison. If you are too curious, you can check it in source code.

Just to remind

  1. empty() -
empty() - 
universal function that can check if var contains something. Supports arrays. Slower than
  1. isset()
isset()
function
  1. isset()
isset()
— the fastest one. can check if var is NOT null and set. Nothing else.
  1. is_null()
is_null()
— simply checks if var is null. nothing else, but very slow according to comparison operators that can do the same

gettype() empty() is_null() isset() boolean : if($x)
$var = «»; string
  1. TRUE
TRUE
  1. FALSE
FALSE
  1. TRUE
TRUE
  1. FALSE
FALSE
$var = null; NULL
  1. TRUE
TRUE
  1. TRUE
TRUE
  1. FALSE
FALSE
  1. FALSE
FALSE
var $x; NULL
  1. TRUE
TRUE
  1. TRUE
TRUE
  1. FALSE
FALSE
  1. FALSE
FALSE
$var is undefined NULL
  1. TRUE
TRUE
  1. TRUE
TRUE
  1. FALSE
FALSE
  1. FALSE
FALSE
$var = array(); array
  1. TRUE
TRUE
  1. FALSE
FALSE
  1. TRUE
TRUE
  1. FALSE
FALSE
$var = false; bool
  1. TRUE
TRUE
  1. FALSE
FALSE
  1. TRUE
TRUE
  1. FALSE
FALSE
$var = true; bool
  1. FALSE
FALSE
  1. FALSE
FALSE
  1. TRUE
TRUE
  1. TRUE
TRUE
$var = 1; int
  1. FALSE
FALSE
  1. FALSE
FALSE
  1. TRUE
TRUE
  1. TRUE
TRUE
$var = 10; int
  1. FALSE
FALSE
  1. FALSE
FALSE
  1. TRUE
TRUE
  1. TRUE
TRUE
$var = 0; int
  1. TRUE
TRUE
  1. FALSE
FALSE
  1. TRUE
TRUE
  1. FALSE
FALSE
$var = -1; int
  1. FALSE
FALSE
  1. FALSE
FALSE
  1. TRUE
TRUE
  1. TRUE
TRUE
$var = «1»; string
  1. FALSE
FALSE
  1. FALSE
FALSE
  1. TRUE
TRUE
  1. TRUE
TRUE
$var = «0»; string
  1. TRUE
TRUE
  1. FALSE
FALSE
  1. TRUE
TRUE
  1. FALSE
FALSE
$var = «-1»; string
  1. FALSE
FALSE
  1. FALSE
FALSE
  1. TRUE
TRUE
  1. TRUE
TRUE
$var = «test»; string
  1. FALSE
FALSE
  1. FALSE
FALSE
  1. TRUE
TRUE
  1. TRUE
TRUE
$var = «true»; string
  1. FALSE
FALSE
  1. FALSE
FALSE
  1. TRUE
TRUE
  1. TRUE
TRUE
$var = «false»; string
  1. FALSE
FALSE
  1. FALSE
FALSE
  1. TRUE
TRUE
  1. TRUE
TRUE
It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterEmail this to someoneShare on LinkedIn

No Comments


Leave a Reply

Your email address will not be published Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code lang=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre lang="" extra="">

*

1 + 15 =