PHP empty function usage Instructions _php tips

Source: Internet
Author: User
Tags parse error php source code
Determine whether a variable is considered to be empty.
But for a long time in my memory, I thought empty should be able to judge whether a thing was empty until one day I was like this "empty (trim ($a))" When the use of the error occurred, And then look it up in the manual. The original empty function can only determine whether a variable is empty, and cannot judge a string (or other value, including function return value) to be empty. Although I know this is only a superficial reason, but no longer to understand the deep-seated reasons.
Recently interested in the PHP kernel, and again think of this problem, so follow the experience of others wrote the following code:
Copy Code code as follows:

<?php
echo "<pre>";
$tokens = Token_get_all (' <?php Empty (trim ($a)); > ');
foreach ($tokens as $t) {
if (Is_array ($t)) {
printf ("%s \%s \ \ n", Token_name ($t [0]), Htmlspecialchars ($t [1]));
}else{
printf ("\t%s\n", $t);
}
}

The resulting output is as follows: (If you don't understand, look at the "List of Parser Tokens" section in the appendix to the PHP manual)
T_open_tag <?php
T_empty EMPTY
(
T_string Trim
(
T_variable $a
)
)

T_close_tag?>
The corresponding token of the variable should be t_variable, and the trim ($a) corresponds to the t_string. So if run directly "empty (trim ($a));" Can cause a run error:
Fatal Error:can ' t use function return value in write context
Then I wanted to empty a string directly. The result is not a run-time error, but a parse error:
Parse error:parse error, expecting ' t_string ' or ' t_variable ' or ' $ ' in ...
(Feeling this error message ... )
The token for the view string corresponds to the t_constant_encapsed_string (string syntax). Although the parameters for the token different, but they are not t_variable, should be in the compile phase of the error is right, why ...
So also learn those high people look at PHP source code to ... But have not read the relevant code, OH, the next time to share ~
Finally, empty is a language structure, not a function!
(and Print,echo,include,require,die and so on are the same language structure, some articles on the internet say that print has a return value is the function is wrong)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.