phphtmlternary-operatorembedded-language

PHP: Is it possible to embed HTML in the middle of a ternary operator somehow?


For example, can I do something like the following?

<? $foobar = 1;
$foobar==0 ? ?>
   <span>html goes here.</span>
<? : ?>
   <span>something else.</span>
<? ; ?>

That code won't work. So i'm guessing it's not possible, or is it?


Solution

  • Alternative using strings.

    <?php
    
    $x = 10;
    ?>
    <p>Some html</p>
    
    <?= $x == 10 ? '<p>true</p>' : '<p>false</p>' ?>
    
    <p>Some other html</p>