+3

PHP

Kindly help me understand this question What is the value of $var? $var = true ? '1' : false ? '2' : '3';

php

3/30/2020 1:03:14 PM

James Mbatia

14 Answers

New Answer

+4

Assignment operation `$var = true` in the left hand ternary operation decides which value becomes the evaluation result. This was either '1' or false. In this case, because `true` was asigned to <$var> the left hand ternary operation results in '1'. As PHP continues processing the right hand ternary operation, PHP needs the '1' to be evaluable as boolean state. So it was converted into a number ('1' -> 1), which translates as boolean `true` value. Lastly, that 1 (converted from '1') be used as condition for right hand ternary operation. At this point, I believe you have figured out why the chained ternary operations as a whole evaluates to 2.

+3

Did the question indicate any parentheses?

+3

David Carroll I corrected my response, my bad, now I don't know what got in to me when I was writing that. Is it possible to have the demo code to also use nested ternary operation as per the original post?

+2

Refer ternary or conditional operator first then you will easily understand. If you can't understand,tell me I will help you

+2

Ipang Here is the error message you were referring to with nested ternary expressions: -------- PHP Code: ---- $var = true ? '1' : false ? '2' : '3'; ---- -------- Error Output: ---- Deprecated: Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)` in ./Playground/file0.php on line 3 ---- https://code.sololearn.com/wIZ7c1TFdVgT/

+2

Thought I’d elaborate a bit, boolean ? x : y; is a ternary condition, you can think of it as a shorthand for if-else statements. The statement you’ve written: $var = true ? ’1’ : false ? ’2’ : ’3’; is the same as writing: if (true) { $var = ’1’; } elseif (false) { $var = ’2’; } else { $var = ’3’; } When viewing it as an if-else statement it’s quite easy to see that the answer would be $var = ’1’; I hope that answers you question and gives some insight on how ternary conditions work.

+1

Geek, This is the syntax for ternary operators (Condition) ? (Statement1) : (Statement2); but the question does not have a condition

+1

Condition will return true or false and if true first statement will be executed else second statement. In your question, there is no condition but directly boolean value is given, so statement 1 will be executed which in your case $var value will become 1

+1

Thanks Geek, but questions picked 2 as the correct answer

+1

I got a warning when trying to run that line in Code Playground. I was asked to tidy up the expression with parentheses. And so I added the parentheses. It turned out (after adding parentheses) the expression evaluates to 2 echo ($var = true ? '1' : false) ? '2' : '3'; And I added output for $var which prints 1. echo $var; So 1 is the answer for your question "What is the value of $var?". But 2 was the result of the expression as a whole.

+1

My worry is how the answer is ending up to be 2

+1

Sonic it had no parenthesis

+1

This is ternary operator ...this is as simple as if ..else loop

+1

David Carroll Yes, that is the error message indeed. Some long message it was difficult to remember 😁 Thanks David 🙏