- URL: https://www.laruence.com/en/2009/12/09/1180.html
- Please include attribution when republishing.
Someone asked me today what the difference between isset and is_null is.
Going by the manual, the two are almost exactly "the same, only opposite"...
So is isset just an inverted alias for is_null?
Well, when it comes to differences, there really are quite a few.
Every one of the differences comes down to this: is_null is a function, isset is a statement.
isset is a statement — like echo and print, it is a language construct built into PHP itself.
is_null is a function — like any ordinary function, it can be called as a variable function.
You might say: fine, fine, I get the difference between a function and a statement, but what the hell is the actual difference?
Well, a statement, a language construct, means it is a statement or identifier supported by the language itself.
for, foreach, continue and so on — they are all "erased" (logically substituted away) at parse time.
Let's look at how the isset statement gets "erased" during parsing.
1. First, during lexical analysis, isset is recognized as the T_ISSET token. 2. Then, during the parsing stage, the isset($var) instruction is reduced to a single opcode: ZEND_ISSET_ISEMPTY_VARS.
You can think of isset as being like a macro in C: it has already been expanded before compilation/execution.
Because of this, the observable behaviour differs in the following ways:
Since is_null is a function, it can be called like this:
<?php $var = NULL; $func = "is_null"; $func($var); ?>
But isset, being a statement, cannot be called that way.
Since is_null is a function, it can take a function's return value as an argument, while isset cannot (of course, PHP could support that if it wanted to — it would just add complexity to the compilation stage):
<?php
is_null(intval("0x45"));
//OK
isset(intval("0x45"));
//PHP Fatal error: Can't use function return value in write context
is_null(NULL);
//OK
isset(NULL);
//PHP Parse error: syntax error
?>
That's enough about isset's shortcomings; let's talk about its advantages:
Because isset is a statement, it is fast!
In a loop of ten million simple checks, the comparison comes out like this:
<?php $a="laruence": isset($a); //time: 1.15s is_null($a); //time: 3.89s ?>
And because isset is called isset, it does not raise a NOTICE when checking an undefined variable:
<?php isset($laruence); //OK is_null($laruence); //PHP Notice: Undefined variable: laruence ?>
So, what's my advice on when to use isset and when to use is_null?
Well, my advice is: let functions do what functions are supposed to do~ Does that sound like a platitude?
isset => is set? => has the variable been assigned (declared)
is_null => is null? => is the variable NULL?
Also, if you do want is_null's semantics, I suggest using "=== NULL" instead. It is consistent with is_null both in meaning and in result, and it's about as fast as isset:
In a loop of ten million simple checks, the comparison comes out like this:
<?php $a="laruence": isset($a); //time: 1.15s is_null($a); //time: 3.88s $a===NULL; //time: 1.22s ?>
Be First to Comment