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
.
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 adds 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 does the comparisons doesn't have to be modified.
Think of a context in which your ternary is used in several dozen files, you will have to go find 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: a database table, json, xml, etc.
In this example, we would work with fixed colors:
Now in various contexts:
Departure:
Let's see another test in another context:
Departure:
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 have9,999,999
files where you have used your beautiful ternary of three options. That beautiful code is now a nightmare for you, because you will have to go looking for those9,999,999
places where you have implemented it to now add the colorgreen
and create a ternary of 4 conditions?But no, you listened to me! :) and you have reflected the logic in the
$colorCodes
. At that moment you will be thankful 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, by adding this to the4=>'green'
. Nothing more. The code will still work fine, whether you have it in one file, or thousands of files.The evolved object is this:
In the App there will be
$data
s withPRIORITY 4
from now on.There is no panic. Your code is portable and there is no problem:
Departure:
Within a minute the code was adapted to the new situation, simply by modifying the
$colorCodes
.Note
I don't use yii... but in C#, when you want to use multiple conditions in a tenary operator, they must be enclosed in parentheses.
So:
So you would have something like this:
The ternary operator is evaluated from left to right . So if you don't group the expressions correctly, you will get an unexpected result.
As @A.Cedano has suggested to you it 's a more correct way to do it, but if you really want to use a ternary operator, you have to use parentheses to separate them something like this:
The PHP tip is [docs] : , it says in Example #4 Non-obvious Ternary Behavior
As a recommendation, to make the code easier to read, I would recommend that you do not do very complex ternary operators, or very complex lambda expressions....
But regarding your question, have you already tried parentheses and the logical operators "and" "or" if it works?
Here are two things I don't know what you want to do:
If what you want to do is this:
If(a==1 and a==2) {code }else{ code }
Try this:
If what you want is to concatenate
if
like this:If(a==1) { if( a==2) {code} }else{ code }
Try putting ternary operators inside parentheses hierarchically
Or you can also do this:
Everything will depend on the type of condition you want to do
think of the following... a ternary operator works as follows
if true is evaluated in the "CONDITION" section, "RESULT TRUE" is executed, If "CONDITION1" is evaluated false, "RESULT FALSE" is executed