So here are the
$y = 0 | 2 | 4; # answer is 6
$x = 0 || 2 || 4; # answer is 2
I know why $y is 6 because it’s using the OR operator on each number and 2 | 4 = 6 but for $x…
Why is it 2?
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.
Because 2 is the first non-falsy item and a logical OR short circuits. It evaluates zero which is false, then 2 which is not false so it is done and returns 2. Consider the output of the following example:
This will output
a_proc: 1. As soon asa_procreturns a true value, the interpreter can stop evaluating since a logical OR oftrueand any value istrue.