php – How to evaluate 3 conditions with a ternary operator?

Question:

Is it possible to evaluate more than 3 conditions in a ternary operator? I am using yii 1.1 and to add color to a cell I use the following condition …

'cssClassExpression'=> '$data->PRIORITY == 1 ? "red" : $data->PRIORITY==2 ? "yellow": "white"',

… but it doesn't work for me since if the priority is one ( 1 ) it always prints it with yellow .

Answer:

Regardless of Yii, which I don't use, instead of using a multiple ternary, I would reflect the logic you want to implement using an object . This brings flexibility to your application, making it more portable and easier to maintain / update.

What will happen to your three-condition ternary if tomorrow you have to deal with 5 or 50 color codes? If you have an object, you just have to add the elements to that object and the code that makes the comparisons does not have to be modified.

Think of a context in which your ternary is used in several dozen files, we will have to look for that code to adapt it to the new condition. With an object, which would be centralized in one place, you just modify the object and that's it. In addition, working with objects allows the code to be more easily integrated into a data model: table in a database, json, xml, etc.

In this example, we would work with fixed colors:

#Toda la lógica de colores de la App se refleja aquí
$colorCodes=(object) array('colors'=>array(1=>'red', 2=>'yellow', 3=>'white'));

Now in various contexts:

#En el archivo número 1 de tu app:
#$data tiene este valor:
$data = (object) array('PRIORITY'=>1,'cssClassExpression'=>'');

#Prueba
$data->cssClassExpression=$colorCodes->colors{$data->PRIORITY};
var_dump($data);

Exit:

object(stdClass)#1 (2) {
  ["PRIORITY"]=>
  int(1)
  ["cssClassExpression"]=>
  string(3) "red"
}

Let's see another test in another context:

#En el archivo número 9,999.999 de tu app:
#$data tiene este valor:
$data = (object) array('PRIORITY'=>2,'cssClassExpression'=>'');

#Prueba
$data->cssClassExpression=$colorCodes->colors{$data->PRIORITY};
var_dump($data);

Exit:

object(stdClass)#1 (2) {
  ["PRIORITY"]=>
  int(1)
  ["cssClassExpression"]=>
  string(3) "yellow"
}

A crucial moment, the App must evolve

One day they give you the news that you have to incorporate a new color code ( green ) and you already have 9,999,999 files where you have used your beautiful three-option ternary. That beautiful code is now a nightmare for you, because you will have to go in search of those 9,999,999 places where you have implemented it to now add the color green and create a ternary of 4 conditions?

But no, you listened to me! 🙂 and you've reflected the logic in the $colorCodes object . At that moment you will give thanks for this answer, because to adapt the context of your App to the new situation, you will only have to write 10 characters! new to the App, adding this to object 4=>'green' . Nothing more. The code will still work fine, whether you have it in one file, or you have it in thousands of files.

The evolved object is this:

$colorCodes=(object) array('colors'=>array(1=>'red', 2=>'yellow', 3=>'white', 4=>'green'));

In the App there will be $data s with PRIORITY 4 from now on.

There is no panic. Your code is portable and there is no problem:

#Estamos en el archivo número 1,000,000,000 de tu App
#Este es exactamente EL MISMO CÓDIGO del archivo 1 :-)
$data->cssClassExpression=$colorCodes->colors{$data->PRIORITY};
var_dump($data);

Exit:

object(stdClass)#1 (2) {
  ["PRIORITY"]=>
  int(4)
  ["cssClassExpression"]=>
  string(5) "green"
}

In a minute the code was adapted to the new situation, simply modifying the $colorCodes object.

Note

Here I put you as an example based on fixed data. It should be said that you can easily implement an option based on default , for example in the case that white should be the color when there are no existing color codes in the object.

Scroll to Top