I know the concepts of Pass-by-value, Pass-by-reference, etc... So, I understand why explicitly defined functions throw a warning when the parameter is not defined.
But, if empty() and isset() are functions, then why doesn't it throw a warning when an undefined variable is passed? Is there some exceptional magic going on here? How do I replicate it?
The isset() function is an inbuilt function in PHP that is used to determine if the variable is declared and its value is not equal to NULL. The empty() function is an inbuilt function in PHP that is used to check whether a variable is empty or not.
empty() function in PHP ? The isset() function is an inbuilt function in PHP which checks whether a variable is set and is not NULL. This function also checks if a declared variable, array or array key has null value, if it does, isset() returns false, it returns true in all other possible cases.
Definition and Usage. The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.
The equivalent of isset($var) for a function return value is func() === null . isset basically does a !== null comparison, without throwing an error if the tested variable does not exist.
empty()
and isset()
are not actually functions.
They're keywords built into the language, and executed by the compiler, which is how the behavior in question is possible - the compiler (unlike the runtime engine, where regular functions execute) already knows if a variable exists or not.
As a side effect, that's why in PHP 5 you couldn't define class methods named empty()
, isset()
, list()
, etc. And you still can't declare classes, or constants via const
using the same names.
Some other languages provide this as a feature called fexpr.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With