php code

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

I used to use 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 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 isset() and empty(). BUT i was surprised a little with is_null(). I supposed that this function will be slower but not in 8 times.

So, when I want to check $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, is_null() the slower than simple comparison operators. Thus, looks like is_null() makes more operations undercover than comparison. If you are too curious, you can check it in source code.

Just to remind

empty() - universal function that can check if var contains something. Supports arrays. Slower than isset() function
isset() — the fastest one. can check if var is NOT null and set. Nothing else.
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 TRUE FALSE TRUE FALSE
$var = null; NULL TRUE TRUE FALSE FALSE
var $x; NULL TRUE TRUE FALSE FALSE
$var is undefined NULL TRUE TRUE FALSE FALSE
$var = array(); array TRUE FALSE TRUE FALSE
$var = false; bool TRUE FALSE TRUE FALSE
$var = true; bool FALSE FALSE TRUE TRUE
$var = 1; int FALSE FALSE TRUE TRUE
$var = 10; int FALSE FALSE TRUE TRUE
$var = 0; int TRUE FALSE TRUE FALSE
$var = -1; int FALSE FALSE TRUE TRUE
$var = «1»; string FALSE FALSE TRUE TRUE
$var = «0»; string TRUE FALSE TRUE FALSE
$var = «-1»; string FALSE FALSE TRUE TRUE
$var = «test»; string FALSE FALSE TRUE TRUE
$var = «true»; string FALSE FALSE TRUE TRUE
$var = «false»; string FALSE FALSE TRUE TRUE
It's only fair to share...Share on FacebookShare on Google+Tweet about this on TwitterEmail this to someoneShare on LinkedIn

Aboutalex

Вэб-программист. Занимаюсь разработкой cервисов, написанием API, вэб-приложений. Интересна разработка приложений для высоконагруженных систем, анализ данных..

Добавить комментарий

Ваш e-mail не будет опубликован. Обязательные поля помечены *

двенадцать + 8 =