+ 3
PHP
Kindly help me understand this question What is the value of $var? $var = true ? '1' : false ? '2' : '3';
19 Respostas
+ 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.
+ 4
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?
+ 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 ??đ¤Łđ¤Łđđđđ
+ 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.
+ 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
David Carroll
Yes, that is the error message indeed. Some long message it was difficult to remember đ
Thanks David đ
+ 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
+ 1
Thank you brother...will do it