Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use conditional logic in PHP EOD?

Tags:

php

Is it possible to put conditional logic inside an EOD string?

$str = <<<EOD

<table>
    <tr>
        <td>
            if ( !empty($var1) ) {
                {$var1}
            } else {
                {$var2}
            }
        </td>
    </tr>
</table>

This doesn't work for me, and it sort of looks like it wouldn't work, but I thought I'd take a stab.

Also, is it EOD or EOT? Both seem to work.

like image 903
1252748 Avatar asked Jan 22 '26 07:01

1252748


1 Answers

No. You cannot use conditionals in heredoc.

Also, is it EOD or EOT?

As long as your beginning and ending strings match you can use anything:

 $x = <<<THOMAS
 Pick a string, any string
 THOMAS;

The doc contains several examples demonstrating this

As to how best to achieve the example you provided, this would be my first inclination:

$td = !empty($var1) ? $var1 : $var2;
$str = <<<EOD
<table>
    <tr>
        <td>
            {$td}
        </td>
    </tr>
</table>
EOD;
like image 145
Frank Farmer Avatar answered Jan 23 '26 22:01

Frank Farmer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!