PHP条件和回显HTML

When I'm using PHP and HTML I normally do it this way:

<?php if ($myvar = 'blah') {
   echo '<div>some html here</div>';
} else {
   echo 'Nothing here';
}
?>

This works but I now have a bunch of html and I need to add a condition and I am trying to avoid having to do this:

<?php if ($myvar = 'blah') {
   echo '<div>some html here</div>';
   echo '<div>some other html here</div>';
   echo '<div>some other html here</div>';
}
?>

Is there a way to wrapper the whole of the html block instead?

You can remove the HTML block and reverse it.

<?php

// get value of $myvar here

?>

<?php if ($myvar == 'blah):?>

<div>some html here</div>
<div>some html here</div>

<?php elseif ($myvar == 'test'):?>

<div>some html here</div>
<div>some html here</div>
<div>some html here</div>

<?php else:?>

<div>some html here</div>

<?php endif;?>

Or you can also use EOD e.g.

echo <<<EOD

<div>some html here</div>
<div>some html here</div>
<div>some html here</div>

EOD;

Just drop out of PHP mode.

<?php if ($myvar = 'blah') { ?>
   <div>some html here</div>
   <div>some other html here</div>
   <div>some other html here</div>
<?php } ?>

Or, if it makes sense to maintain it separately, move the data to another file:

<?php if ($myvar = 'blah') { 
    include('foo.php'); 
} ?>

Or, use heredoc syntax:

if ($myvar = 'blah') {
echo <<<EOT
<div>some html here</div>
<div>some other html here</div>
<div>some other html here</div>
EOT;
}

You can do this instead. The echo can span multiple lines

<?php if ($myvar = 'blah') {
   echo '<div>some html here</div>
   <div>some other html here</div>
   <div>some other html here</div>';
}
?>

Or you could use the multiple parameters of the echo function

<?php if ($myvar = 'blah') {
   echo '<div>some html here</div>',
   '<div>some other html here</div>',
   '<div>some other html here</div>';
}
?>

I sometimes do it this way:

<?php    
if ($myvar = 'blah') {
?>
   <div>some html here</div>
   <div>some other html here</div>
   <div>some other html here</div>
<?php
   }
?>

do you mean like this?

original

echo '<div>some html here</div>';
echo '<div>some other html here</div>';
echo '<div>some other html here</div>';

combine into one variable

$html = '<div>some html here</div><div>some other html here</div><div>some other html here</div>'

echo $html;

variable concatenation

$html = '<div>some html here</div>';
$html .= '<div>some other html here</div>';
$html .= '<div>some other html here</div>';

echo $html;

close php tags

?>
<div>some html here</div>
<div>some other html here</div>
<div>some other html here</div>
<?php

The only reliable way to output HTML is to drop out of PHP mode:

<?php if ($myvar = 'blah') { ?>
   <div>some html here</div>
   <div>some other html here</div>
   <div>some other html here</div>
<?php } ?>

No HTML tag should be printed out other than this way.

To store resulting HTML in a variable you can either use above method in conjuncion with output buffering, or use a heredoc/concatenation.