I tried this but only got a syntax error:
<?php
$a = true;
$str = <<< EOF
{$a ? 1 : 2}
EOF;
echo $str;
Is it possible to use such kind of conditional statement inside heredoc?
Nope. PHP string interpolation is, unfortunately, not quite that robust. You'll either have to concatenate two strings, or assign that little bit of logic to another variable ahead of time.
<?php
$a = true;
$b = $a ? 1 : 2;
$str = <<<EOF
Hello, world! The number of the day is: $b
EOF;
echo $str;
I would say no.
See this related question on why you can't do function calls and possible workarounds: Calling PHP functions within HEREDOC strings
The crux of it is that you will probably have to assign your ternary operator to a variable before the heredoc.
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