+ 3

PHP

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

30th Mar 2020, 1:03 PM
James Mbatia
James Mbatia - avatar
17 Respuestas
+ 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.
30th Mar 2020, 3:26 PM
Ipang
+ 4
Did the question indicate any parentheses?
31st Mar 2020, 1:12 AM
Sonic
Sonic - avatar
+ 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?
3rd Apr 2020, 8:03 AM
Ipang
+ 3
Help.me too brother. M also having problem with my php file ...will send the code ..please help . Together we can make it better ..wanna help brother ??🤣🤣🙂🙂🙂🙂
18th Jul 2020, 6:25 AM
Nar Bdr Kharka
Nar Bdr Kharka - avatar
+ 3
Nar Bdr Kharka The input arguments aren't in a format that PHP can parse. Are you trying pass in 3 separate arguments or a single argument object with 3 properties? Anyway, you really should post this in your own question post as this is hijacking another thread. Don't respond to my follow up questions here. Please, just ask the same question in a new post and mention my name in the question or share a link to your new post. I'll then repost my follow up questions in your new thread and continue the discussion there.
18th Jul 2020, 6:56 AM
David Carroll
David Carroll - avatar
+ 2
Refer ternary or conditional operator first then you will easily understand. If you can't understand,tell me I will help you
30th Mar 2020, 1:16 PM
Geek
Geek - avatar
+ 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/
3rd Apr 2020, 12:33 PM
David Carroll
David Carroll - avatar
+ 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.
18th May 2020, 6:52 PM
Victor Andersson
Victor Andersson - avatar
+ 1
Geek, This is the syntax for ternary operators (Condition) ? (Statement1) : (Statement2); but the question does not have a condition
30th Mar 2020, 1:27 PM
James Mbatia
James Mbatia - avatar
+ 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
30th Mar 2020, 2:18 PM
Geek
Geek - avatar
+ 1
Thanks Geek, but questions picked 2 as the correct answer
30th Mar 2020, 2:26 PM
James Mbatia
James Mbatia - avatar
+ 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.
30th Mar 2020, 2:34 PM
Ipang
+ 1
My worry is how the answer is ending up to be 2
30th Mar 2020, 2:39 PM
James Mbatia
James Mbatia - avatar
+ 1
Sonic it had no parenthesis
31st Mar 2020, 2:08 AM
James Mbatia
James Mbatia - avatar
+ 1
David Carroll Yes, that is the error message indeed. Some long message it was difficult to remember 😁 Thanks David 🙏
3rd Apr 2020, 12:52 PM
Ipang
+ 1
<?php inputElement(placeholder:"Book Name", name:"book_name", value:" ");?> When I run the above code in inded.php from component.php. it is saying parse errors..what is it my dear brother ..if you dont get my questions .tell me ..will be details
18th Jul 2020, 6:46 AM
Nar Bdr Kharka
Nar Bdr Kharka - avatar
+ 1
Thank you brother...will do it
18th Jul 2020, 6:57 AM
Nar Bdr Kharka
Nar Bdr Kharka - avatar