Home  >  Article  >  Backend Development  >  Instructions for using php empty function_PHP tutorial

Instructions for using php empty function_PHP tutorial

WBOY
WBOYOriginal
2016-07-21 15:45:131032browse

Determine whether a variable is considered to be empty.
But in my memory, for a long time, I always thought that empty should be able to determine whether a thing is empty, until one day I was like this "empty(trim( An error occurred when using $a))", and then I checked the manual and found out that the empty function can only determine whether a variable is empty, but cannot determine whether a string (or other value, including function return value) is empty. Although I knew that this was only a superficial reason, I did not understand the deeper reasons.
I recently became interested in the PHP kernel and thought of this problem again, so I wrote the following code based on other people’s experience:

Copy code Code As follows:

echo "
"; 
$tokens = token_get_all('foreach($tokens as $t){
if (is_array($t)){
printf("%s t %s n", token_name($t[0]), htmlspecialchars ($t[1]));
}else{
} printf("t%sn", $t);
}
}

The result output is as follows : (If you don’t understand this, please take a look at the “List of Parser Tokens” section in the appendix of the php manual)
T_OPEN_TAG T_EMPTY empty
(
T_STRING trim
(
T_VARIABLE $a
)
)

T_CLOSE_TAG ?>
The token corresponding to the variable should be T_VARIABLE, and trim($a) corresponds to T_STRING. So if you run "empty(trim($a));" directly, it will cause a running error:
Fatal error: Can't use function return value in write context
What happens if I want to directly empty a string later? Woolen cloth? The result is not a running error, but a parsing error:
Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `'$'' in ...
(This error message feels confusing . . . )
The token corresponding to the view string is T_CONSTANT_ENCAPSED_STRING (string syntax). Although the parameters are different for the token, they are not T_VARIABLE. An error should have occurred during the compilation stage. Why. . .
So I followed the example of those experts and looked at the source code of PHP. . . But I haven’t read the relevant code yet, haha, I’ll share it next time~
One last time, empty is a language structure, not a function!
(Like print, echo, include, require, die, etc., they are all language structures. Some articles on the Internet say that if print has a return value, it is wrong to call it a function)

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/320397.htmlTechArticleDetermine whether a variable is considered to be empty. But in my memory, for a long time, I always thought Empty should be able to determine whether something is empty until there is...
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
Previous article:Talking about PHP generating static page analysis template + cache + writing files_PHP tutorialNext article:Talking about PHP generating static page analysis template + cache + writing files_PHP tutorial

Related articles

See more