Home  >  Article  >  Backend Development  >  print is not a function

print is not a function

藏色散人
藏色散人forward
2019-06-03 17:07:152465browse

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")) {
    ;
}

What do you expect this code to output?

Actually The output is:

4
111

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")))) {
  ;
}

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")) {
    ;
}

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!

Statement:
This article is reproduced at:laruence.com. If there is any infringement, please contact admin@php.cn delete