0

Can I use multiple if else in this format?

$data->frm_status==1? "Incomplete" :"other" 

I have tried with different ways . like this

$data->frm_status==1? "Incomplete" $data->frm_status==2 ?"Analysis Done":"other"

$data->frm_status==1? "Incomplete" ? $data->frm_status==2 ?"Analysis Done":"other"

This is the error. I am unable to search this on google because I don't know the name for this syntax.

although i know i can use this

if()
{
    //
}else if()
{
  // 
}else
{
   //
}
Jonny C
  • 1,943
  • 3
  • 20
  • 36
Manoj Dhiman
  • 5,096
  • 6
  • 29
  • 68
  • *"but this is the error "* What is? – GolezTrol May 11 '15 at 12:23
  • Would this be readable to you if you had the right syntax? If not, then use a plain `if` statement. – mario May 11 '15 at 12:24
  • Parentheses are your friend! Note that the [ternary operator](http://php.net/manual/en/language.operators.precedence.php) is left associative. – Rizier123 May 11 '15 at 12:24
  • "Ternary operator" on google – David Ansermot May 11 '15 at 12:25
  • It's called the [`ternary operator`](http://php.net/manual/en/language.operators.comparison.php#language.operators.comparison.ternary) and it **is not** the same as an if/then/else statement. Use parenthesis at will to control the order of the evaluation. – axiac May 11 '15 at 12:25
  • what is the difference @axiac – Manoj Dhiman May 11 '15 at 12:26
  • `?:` is an operator. It connects (2 or) 3 expressions to generate a bigger expression. Think of it like a strange cousin of the addition operator (`+`). An expression is evaluated to a value; some of the operators it contains can also have the side-effect of changing the variables they connect (the assignment operators do that). `if/then/else`, on the other hand, is a flow control structure. It controls what statements are executed. In short, you cannot put a `foreach` statement into an expression (with or w/o the ternary operator) but you can put it in an `if/then/else` block. – axiac May 11 '15 at 12:35
  • A little off topic: http://fabien.potencier.org/article/48/the-php-ternary-operator-fast-or-not – medowlock May 11 '15 at 12:38

1 Answers1

2

Ternary operator isn't good choice for multiple (else)ifs:

echo $data->frm_status == 1 ? "Incomplete" : ($data->frm_status == 2 ? "Analysis Done" : "other");

The better approach is:

if ($data->frm_status == 1) {
    echo 'Incomplete';
} elseif ($data->frm_status == 2) {
    echo 'Analysis Done';
} else {
    echo 'other';
}

OR switch:

switch ($data->frm_status) {
    case 1:
        'Incomplete';
        break;
    case 2:
        'Analysis Done';
        break;
    default: 
        'Other';
        break;
}
pavel
  • 26,538
  • 10
  • 45
  • 61
  • your answer is working . But may i know the difference between ternary operator and nested if else if?? – Manoj Dhiman May 11 '15 at 12:28
  • @ris: it's the same, no different, just other syntax. Ternary operator is better for short construct like `echo $a == 2 ? 'true' : 'false'`, `if-elsif-else` for longer construcitons. The result is the same in both cases. In other words, ternary operator is alternative syntax for `if` condition. – pavel May 11 '15 at 12:31
  • It's not exactly the same. An if statement executes code if a certain condition is met. As you can see in this answer, a value is echoed. A ternary expression is an expression that evaluates into a value. By itself it does nothing, but you can assign or use that value to do something. For that reason I also think the `switch` statement is wrong. It contains loose strings that don't do anything by themselves. – GolezTrol May 11 '15 at 12:51