views:

349

answers:

2

I am trying to set a flag to show or hide a page element, but it always displays even when the expression is false.

$canMerge = ($condition1 && $condition2) ? 'true' : 'false';
...
<?php if ($canMerge) { ?>Stuff<?php } ?>

What's up?

A: 

The value of 'false' is true. You need to remove the quotes:

$canMerge = ($condition1 && $condition2) ? true : false;
Flubba
This so seems like a reputation-farming attempt. A 3k-rep user who can't differentiate between a string and a boolean, who answers his question a minute after he makes it?
orlandu63
Yeah, it wasn't even a useful question to begin with.
Ed Swangren
Thanks for your comments Orlando and Ed. This was an issue that caught me out and I posted it for mine and other's benefit. Answering your own questions is perfectly fine per the FAQ. Maybe you should go read it again if it's been a while...
Flubba
+17  A: 

This is broken because 'false' as a string will evaluate to true as a boolean.

However, this is an unneeded ternary expression, because the resulting values are simple true and false. This would be equivalent:

$canMerge = ($condition1 && $condition2);
Rudd Zwolinski