In PHP, is there a way to concatenate two strings using a ternary conditional?
<?= 'something' . (true) ? 'else' : 'not'; ?>
When I try that, all I get is else
rather than the desired something else
.
Just put brackets around the entire ternary operator like this:
<?= 'something' . ((true) ? ' else' : ' not'); ?>
Why do you have to do that?
The answer is: operator precedence
See the manual for more information: http://php.net/manual/en/language.operators.precedence.php
Yes, you need to put your ternary in brackets though. Try this:
<?php echo 'something '.((true) ? 'else' : 'not'); ?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With