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?
original title: "php - Why is my ternary expression not working?"
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?
Sto cercando di impostare un flag per mostrare o nascondere un elemento di pagina, ma viene sempre visualizzato anche quando l'espressione è falsa. $ canMerge = ($ condition1 && $ condition2)? 'vero falso'; ... <?...
Questo è il riepilogo dopo la traduzione, se è necessario visualizzare la traduzione completa, fare clic sull'icona "traduci"
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:
The value of 'false' is true. You need to remove the quotes:
Seems to me a reasonable question especially because of the discrepancy in the way PHP works.
For instance, the following code will output 'its false'
then