Below is something strange, I didn’t get
if (print("foo") || print("bar")) {
// "foo" has been printed.
}
Why the output is 1?
Could please explain?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
It’s because PHP is a ridiculous language.
printis not a normal function, it’s a language construct. This line is actually parsed as:And
("foo") || print("bar")is an expression which evaluates to1. The string"foo"in a boolean context is true, so the||operator yields1.If you explicitly parenthesize the expression the way one would expect it to be parsed:
Then the output is what you would expect: