Home  >  Article  >  Backend Development  >  bohemian rhapsody php empty function instructions

bohemian rhapsody php empty function instructions

WBOY
WBOYOriginal
2016-07-29 08:40:321253browse

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($a ))" when using it, and then I checked the manual and found out that the empty function can only judge whether a variable is empty, but cannot judge 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 the code The code is as follows:


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


The result output is as follows: (If you don’t understand this, please take a look at the "List of Parser Tokens" part of 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 empty a string directly later? 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 seems annoying...)
The token corresponding to the 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 checked 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)
The above introduces the instructions for using the bohemian rhapsody php empty function, including the content of bohemian rhapsody. I hope it will be helpful to friends who are interested in PHP tutorials.


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