Home > Backend Development > PHP Tutorial > print is not a function

print is not a function

藏色散人
Release: 2023-04-06 20:14:01
forward
2489 people have browsed it

print is not a function

This comes from a seemingly weird question:

if (print("1\n") && print("2\n") && print("3\n") && print("4\n")) {
    ;
}
Copy after login

What do you expect this code to output?

Actually The output is:

4
111
Copy after login

Many times we ignore that print is a grammatical structure (language constructs), it is not a function, and the list of parameters does not require parentheses (even if you write Parentheses, parentheses will also be ignored during the syntax analysis stage), it is just an "expression (expr)" that always returns 1:

expr :
   T_PRINT expr
 | '(' expr ')'
;
所以其实上面的代码在php看来是:
if (print ("1\n" && print ("2\n" && print ("3\n" && print "4\n")))) {
  ;
}
Copy after login

So, output 4, and then output "3\n" The result of && print is 1, then "2\n" && 1 is output, and finally "1\n" && 1

And if we want to achieve the intended intention of the above code, we should Write this:

if ((print "1\n") && (print "2\n") && (print "3\n") && (print "4\n")) {
    ;
}
Copy after login

The above is the detailed content of print is not a function. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:laruence.com
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template