PHP条件和回显HTML

PHP条件和回显HTML,php,Php,当我使用PHP和HTML时,我通常是这样做的: <?php if ($myvar = 'blah') { echo '<div>some html here</div>'; } else { echo 'Nothing here'; } ?> <?php if ($myvar = 'blah') { ?> <div>some html here</div> <div>some o

当我使用PHP和HTML时,我通常是这样做的:

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

这是可行的,但我现在有一堆html,我需要添加一个条件,我正试图避免这样做:

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


是否有办法包装整个html块?

退出PHP模式即可

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

这里有一些html
这里还有其他一些html
这里还有其他一些html
或者,如果需要单独维护,请将数据移动到另一个文件:

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

或使用:

if($myvar='blah'){

echo您可以这样做。echo可以跨越多条线路

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

或者可以使用echo函数的多个参数

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

我有时会这样做:

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

这里有一些html
这里还有其他一些html
这里还有其他一些html

您可以删除HTML块并将其反转

<?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;?>

你是说像这样吗

原创

echo '<div>some html here</div>';
echo '<div>some other html here</div>';
echo '<div>some other html here</div>';
echo'somehtml here';
回声'一些其他html在这里';
回声'一些其他html在这里';
组合成一个变量

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

echo $html;
$html='some html here some other html here some other html here some other html here'
echo$html;
变量串联

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

echo $html;
$html='some html here';
$html.='此处有其他html';
$html.='此处有其他html';
echo$html;
关闭php标记

?>
<div>some html here</div>
<div>some other html here</div>
<div>some other html here</div>
<?php
?>
这里有一些html
这里还有其他一些html
这里还有其他一些html

输出HTML的唯一可靠方法是退出PHP模式:

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

这里有一些html
这里还有其他一些html
这里还有其他一些html
除此之外,不应打印任何HTML标记


要将生成的HTML存储在变量中,您可以使用上述方法与输出缓冲结合使用,也可以使用heredoc/串联。

您可能希望在if语句中使用相等测试而不是赋值。