0

使用 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>";?>
4

2 回答 2

0

你可以做这样的事情,通过隐藏 div

  <div id="someID" <?php if (!$Subject){?>style="display:none"<?php } ?>>
于 2013-10-20T08:11:37.903 回答
0

很难假设您问题的整个背景(最好多写一点关于值的$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>';
}
于 2013-10-20T12:32:16.550 回答