Why is my ternary expression not functioning?

I am attempting to set a flag to show or hide a web page component, yet it constantly presents also when the expression is incorrect.

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

What's up?

2019-05-03 17:51:40
Source Share
Answers: 2

The value of 'incorrect' holds true. You require to remove the quotes :

$canMerge = ($condition1 && $condition2) ? true : false;
2019-05-07 18:19:15

This is damaged due to the fact that 'incorrect' as a string will certainly review to real as a boolean.

Nonetheless, this is an unnecessary ternary expression, due to the fact that the resulting values are straightforward real and also incorrect. This would certainly be equal :

$canMerge = ($condition1 && $condition2);
2019-05-07 18:07:37