0

I've tried the shorthand but it's now working properly on me please help me to solve this problem thank you.

$acc = "free";

echo $limit = $acc == "free" ? 5 : $acc == "muyip" ? 50 : 1000;

My expected result is 5 but the result is 50

  • 2
    PHP 7.4 gives a hint _Deprecated: Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c ? d : e)`_ – AbraCadaver Dec 09 '19 at 12:58
  • 2
    From manual: *Note: It is recommended that you avoid "stacking" ternary expressions. PHP's behaviour when using more than one ternary operator within a single statement is non-obvious* – Andreas Dec 09 '19 at 12:59
  • Thank you so much It's working properly now – Yecebain Kitbeja Dec 09 '19 at 12:59
  • A switch/case statement would probably be much better and much cleaner here. – Qirel Dec 09 '19 at 13:00

1 Answers1

2

use brackets as below

$acc = "free";

echo $limit = $acc == "free" ? 5 : ($acc == "muyip" ? 50 : 1000);
Devsi Odedra
  • 5,244
  • 1
  • 23
  • 37