Home >Backend Development >PHP Tutorial >php Note: empty() only checks variables as anything error_PHP tutorial
Today I found a tip when using the empty() function to determine whether a variable is null. Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work: empty(trim ($name)). It was wrong, and it took me half a day to find the problem.
When you use empty to check the result returned by a function, the following fatal error will be reported:
Fatal error: Can't use function return value in write context in : .............
For example:
echo empty(yourfunction(xx, oo));
Go to the PHP manual and see the following text where the empty function is described:
Note: empty() only checks variables as anything else will result in a parse error. In other words, the following will not work:empty(trim($name)).
empty() only tests variables, testing anything that is not a variable will result in a parsing error!
Therefore, we cannot use empty to directly detect the value returned by the function. We need to assign the return value of the function to a variable first, and then use empty to detect the variable.
So, we can write it in the following form:
$return= yourfunction(xx, oo);
echo empty(return);