How do empty() and isset() avoid warnings on undefined variables?

Viewed 555

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?

1 Answers

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.

Related