I'm not sure whether what I'm doing is fundamentally wrong or whether I've just had a long day and am missing something obvious...
I'm setting array values dynamically like so:
$criteria = array(
'is_blue' => get_color($color_1) == ('Dark Blue' OR 'Light Blue') ? TRUE : FALSE,
'is_red' => get_color($color_2) == ('Dark Red' OR 'Light Red') ? TRUE : FALSE
);
When I try the following line just run by itself:
echo get_color($color_1);
I get Dark Green
. So you would expect it to evaluate as FALSE
in $criteria['is_blue']
.
But - $criteria['is_blue']
is TRUE
.
Why is this? Am I misunderstanding the Ternary operator?
Thanks!
You are using the ternary operator correctly, but the if
portion is malformed.
You are essentially testing if get_color($color_1)
is equal to true
because 'Dark Blue' OR 'Light Blue'
equals true
.
You'll need to test each value independantly, like this:
$criteria = array(
'is_blue' => get_color($color_1) == 'Dark Blue' OR get_color($color_1) == 'Light Blue' ? true : false,
'is_red' => get_color($color_2) == 'Dark Red' OR get_color($color_2) == 'Light Red' ? true : false
);
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments