Home  >  Article  >  Backend Development  >  What to do if php empty error occurs

What to do if php empty error occurs

藏色散人
藏色散人Original
2020-08-29 10:55:192355browse

php empty error is because empty only detects variables. Detecting anything that is not a variable will result in a parsing error. The solution is to directly detect the value returned by the function without using empty. The code is such as "$length=strlen ('test');echo empty($length);".

What to do if php empty error occurs

Recommendation: "PHP Video Tutorial"

Solution to error reported by PHP empty function

Solution to the error reported by PHP empty function when detecting a non-variable.

When developing in PHP, when you use empty to check the result returned by a function, an error will be reported:

Fatal error: Can't use function return value in write context

For example The following code:

<?php 
echo empty(strlen(&#39;test&#39;));

Go to the PHP online manual to view. In the description of the empty function, there is the following text:

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)).

Conclusion: empty() only detects variables and detects any non- Variables will cause parsing errors!

Therefore, we cannot use empty to directly detect the value returned by the function. The solution to the above example is as follows:

<?php
$length = strlen(&#39;test&#39;);
echo empty($length);

The above is the detailed content of What to do if php empty error occurs. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn