php-根据条件插入html标记

php-根据条件插入html标记,php,html,Php,Html,使用php,我希望根据条件是否满足,在一些内容周围包装div标记。目前我正在这样做,如下例所示。有更好的方法吗?这似乎不是很专业 <?php if($Subject) echo "<div id='someID'>";?> <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's

使用php,我希望根据条件是否满足,在一些内容周围包装div标记。目前我正在这样做,如下例所示。有更好的方法吗?这似乎不是很专业

<?php if($Subject) echo "<div id='someID'>";?>

    <p>Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem
 Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown
 printer took a galley of type and scrambled it to make a type specimen book. It has 
survived not only five centuries, but also the leap into electronic typesetting, remaining
 essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets
 containing Lorem Ipsum passages, and more recently with desktop publishing software like
 Aldus PageMaker including versions of Lorem Ipsum.</p>

<?php if($Subject) echo "</div>";?>

您可以通过隐藏div来执行类似的操作

  <div id="someID" <?php if (!$Subject){?>style="display:none"<?php } ?>>
>

很难假设问题的全部背景(最好再多写一点关于
$subject
的值来自何处以及可能是什么值)

但是假设
$subject
可以是一组指定的值,您可以尝试以下操作:

$text = 'Some arbitrary text';

$allowedWrappers = array(
    'one'   => 'someID',
    'two'   => 'anotherID',
    'three' => 'yetAnotherID'
);

if(isset($subject) && in_array($subject, $allowedWrappers)) {
    echo '<div id="'.$allowedWrappers[$subject].'"><p>'.$text.'</p></div>';
} else {
    echo '<p>'.$text.'</p>';
}
$text='一些任意文本';
$allowedWrappers=数组(
“一个”=>“某个ID”,
“两个”=>“另一个”,
“三个”=>“yetAnotherID”
);
if(isset($subject)和in_数组($subject,$allowedWrappers)){
回显“”.$text.“

”; }否则{ 回显“”.$text.“

”; }
我不认为这很不专业。但是你必须告诉我们关于你的案例的更多信息才能确定。使用模板引擎。它只适用于两个页面,所以我不愿意只对两个页面采用模板引擎方法。关于我的案例,基本上,如果满足条件,我会将页面拆分为选项卡式内容,因为这意味着需要另一个部分。目前它运行良好,我只是不喜欢让php代码在我的页面上分散回结束标记。谢谢,但如果满足条件,我不想隐藏内容,我只希望在满足条件的情况下添加div标记。