-1
<?php
$var = 4;
echo $current = ($var > 2) ? "gr than 2" : ($var > 6) ? "gr than 6" : "not gr than 2 or 6";
?>

for the above code, it always returns - gr than 6. Can someone please suggest what did I wrong?

8
  • 2
    use parentheses or just use if-then-else Commented Jun 7, 2017 at 11:03
  • 1
    echo $current = ($var > 2) ? (($var > 6) ? "gr than 6" : "gr than 2") : "not gr than 2 or 6"; Commented Jun 7, 2017 at 11:05
  • 3
    Using ternary operators like this isn't something i'd recommend, it very quickly becomes hard to see the logic and therefor debugging it, instead of using ternary operators like this you should considering switching back to simple if / if else / else Commented Jun 7, 2017 at 11:05
  • 1
    I don't recommend multiple ternary conditions - it looks messy and is really hard to read. I recommend a switch statement or if/elseif/else Commented Jun 7, 2017 at 11:05
  • 1
    Don't bother about complicated..You can use multiple conditions in ternary..try man..it will makes you good programmer..Just try my answer.. Commented Jun 7, 2017 at 11:13

6 Answers 6

3

The code will be executed front to back. So first

<?php
($var > 2) ? "gr than 2" : ($var > 6)
?>

will result in "gr than 2".

Then the next questionmark will result in gr than 6, because "gr than 2" is equal to true.

Also because of the above it would be good to notice that > 6 and > 2 are both greater than 2, so the whole line is actually quite pointless the way it is written.

The solution would be something like:

<?php
$var = 4;
echo $current = ($var < 2 ? "not gr than 2 or 6" : ($var > 6 ? "gr than 6" : "gr than 2"));
?>

* Edit: *

Thank you for the upvotes. When looking again at this I got lost in my own post, because the logic is so complex. So for others reading this:

The logic the OP posted can be simplified to the following:

<?php
echo true ? "first" : false ? "second" : "third";

The OP would expect this to result in first. However, it does result in second because first the first part is being executed, and because that part is true the outcome is "second".

Sign up to request clarification or add additional context in comments.

2 Comments

I was about to send my answer.. +1 For mentioning because "gr than 2" is equal to true and showing how it's 'grouped'.
Also +1 for pointing out that just adding parentheses would make it unable to reach "gr than 6" as it would fell into "gr than 2".
2

use below code

<?php
$var = 4;
echo $current = (($var > 2) ? "gr than 2" : (($var > 6) ? "gr than 6" : "not gr than 2 or 6") );
?>

1 Comment

Still you should check if $var > 6 before $var > 2 because in your "solution" if $var equals 8 you'll end up with "gr than 2"
1

This.

echo $current = ($var > 2) ? ($var >6)? "gr than 6":"lower than 6" : "lower than 2 or 6";

Comments

1

You can use () for each conditions..try it..

echo $current = (($var > 2) ? "gr than 2" : (($var > 6) ? "gr than 6" : "not gr than 2 or 6"));

1 Comment

Okai..never give up...we need little code high performance..Be perfect in the complicated way..
1

Set the priority

    <?php
$var = 4;
echo $current = ($var > 2) ? "gr than 2" : ( ($var > 6) ? "gr than 6" : "not gr than 2 or 6" );
?>

Comments

1

The solution is to use parentheses to group your operators and also alter the order of the conditions a bit:

echo $current = ($var > 2) ?
    (($var > 6) ? "gr than 6" : "gr than 2") : 
    "not gr than 2 or 6 (smaller than 2)";

The problem in your version is that by default it gets grouped like this:

echo $current = (($var > 2) ? "gr than 2" : ($var > 6)) ? 
  "gr than 6" : 
  "not gr than 2 or 6";

Which is equal to:

echo $current = ("gr than 2") ? 
  "gr than 6" : 
  "not gr than 2 or 6";

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.