Yes it is possible but DO NOT CHAIN TERNARIES in PHP because it makes the evaluations of the expression from the left different from most languages which in practice returns strange results (the middle one) realize that the other answers made the chaining but always with parentheses to define the priority.
Ternary example that returns the middle result:
$ativo = 1;
$r = $ativo == 0 ? 'Médico(a) Desativado(a)' :
$ativo == 1 ? 'Médico(a) Ativo(a)' :
$ativo == 2 ? 'Outro' : 'else final';
echo $r;
The way out : Outro
To fix this side Effect, in PHP 5.3 the operator was introduced Elves (?:
), it returns the first or last part of the expression. The simplest way I see is to make an array with status:
$descricao = array(0 => 'Médico(a) Desativado(a)', 1 => 'Médico(a) Ativo(a)', 2 =>'Outro');
$ativo = 5;
echo $descricao[$ativo] ?: 'Resultado padrão';
In PHP 7 you can use null coalescing (??
):
echo $descricao[$ativo] ?? 'Resultado padrão';
Doesn’t chain ternaries mainly in php.
– rray
I’ll take the advice then. It was more of a question as to whether and how @rray
– Miguel Carlos