views:

27

answers:

2

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?

+2  A: 

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;
Matchu
+2  A: 

I would say no.

See this related question on why you can't do function calls and possible workarounds: http://stackoverflow.com/questions/104516/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.

Graphain