-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?

  • 2
    use parentheses or just use if-then-else – Edwin Jun 07 '17 at 11:03
  • 1
    `echo $current = ($var > 2) ? (($var > 6) ? "gr than 6" : "gr than 2") : "not gr than 2 or 6";` – kRicha Jun 07 '17 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` – Epodax Jun 07 '17 at 11:05
  • why using so big code? – Deepak Kumar Jun 07 '17 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 – treyBake Jun 07 '17 at 11:05
  • @kRicha I find my answer, Thanks. – Niladri Banerjee - Uttarpara Jun 07 '17 at 11:09
  • 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.. – Janen R Jun 07 '17 at 11:13
  • 1
    @Jana But that is so hard to read you spend more time figuring out whats actually being done, instead of typing a `if/elseif` structure.. Complicated isn't always better! The *simpler* is often better! – Qirel Jun 07 '17 at 11:14

6 Answers6

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".

vrijdenker
  • 1,371
  • 1
  • 12
  • 25
  • I was about to send my answer.. +1 For mentioning _because "gr than 2" is equal to true_ and showing how it's 'grouped'. – FirstOne Jun 07 '17 at 11:11
  • 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". – Rafalon Jun 07 '17 at 11:15
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") );
?>
Nadeem Shaikh
  • 289
  • 2
  • 13
  • 1
    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"` – Rafalon Jun 07 '17 at 11:31
1

This.

echo $current = ($var > 2) ? ($var >6)? "gr than 6":"lower than 6" : "lower than 2 or 6";
Shahmee
  • 299
  • 1
  • 2
  • 16
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"));

Janen R
  • 729
  • 10
  • 21
  • 1
    Okai..never give up...we need little code high performance..Be perfect in the complicated way.. – Janen R Jun 07 '17 at 11:26
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" );
?>
bahek2462774
  • 662
  • 7
  • 12
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";
Zoli Szabó
  • 4,366
  • 1
  • 13
  • 19