if / else condition with elseif [duplicate]

-2

I have a question about the IF and ELSE condition in PHP when used this way:

($str == "1") ? TRUE : FALSE;

The good thing is that if I have a variable already created, such as $ str_principal, I give an equal and use it to give a logic using the IF and ELSE without having to use the if ($ str), understand?

The question itself is, in this way, is there any way to use the "elseif"? That is:

($str == "1") ? 'ITEM1' ($str == "1") ? 'ITEM2' : 'NOITEM';

Explaining better, the if statement (ifif) condition. I would like to know if in the case presented at the beginning it is possible to use elseif.

Thank you!

    
asked by anonymous 30.08.2018 / 20:51

2 answers

-3

Place the second condition inside ()

($str == "1") ? 'ITEM1' : (($str == "1") ? 'ITEM2' : 'NOITEM');
    
30.08.2018 / 20:55
-3

The name of this is ternary operator.

You can do this:

($str == "1") ? TRUE : (($str == "2") ? TRUE : FALSE);

Where in the place of false, which would be the else, you repeat the operation.

    
30.08.2018 / 20:56