Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

function

php

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?

like image 885
BlackPanther Avatar asked Oct 02 '17 07:10

BlackPanther


People also ask

What is the difference between empty () and isset () functions?

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.

Does Isset check if empty?

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.

What does function isset () do?

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.

What can I use instead of isset in PHP?

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.


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.

like image 99
Narf Avatar answered Nov 15 '22 17:11

Narf