PHP Ternary Operator Misunderstanding?

Jack

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!

showdev

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

understading the ternary operator in php

From Dev

evaluation of ternary operator in php

From Dev

Ternary Operator in PHP

From Dev

evaluation of ternary operator in php

From Java

How to write a PHP ternary operator

From Dev

PHP ternary operator syntax error

From Dev

Understanding PHP's ternary operator

From Dev

ternary operator in php with echo value

From Dev

Understanding PHP's ternary operator

From Dev

PHP ternary operator syntax error

From Dev

Conditional ternary operator malfunctions (PHP)

From Dev

Ternary operator inside calculation PHP

From Dev

Ternary operator in PHP generated table

From Java

PHP ternary operator vs null coalescing operator

From Dev

How to use continue keyword in a ternary operator in php

From Dev

convert some lines of php statement into ternary operator

From Dev

Is there a PHP like short version of the ternary operator in Java?

From Dev

Implement inline ternary operator in embedded HTML in PHP

From Dev

How to use php ternary operator in the parameter of a function?

From Dev

How to use continue keyword in a ternary operator in php

From Dev

PHP selected dynamic option with Ternary Operator

From Dev

convert some lines of php statement into ternary operator

From Dev

Can logical operator be used with in ternary operators in PHP

From Dev

PHP inline statement using ternary logic operator "?:"

From Dev

Misunderstanding of the cons operator

From Dev

+ operator in a ternary operator

From Dev

Ruby ternary operator (or) or operator

From Dev

Ternary operator and increment operator

From Dev

Ternary Operator(Elvis Operator) ?: