php - 如何为 div id 设置条件

标签 php html css wordpress joomla2.5

我正在尝试通过 id 的条件?如何实现以下条件

if ( $vm->check( "pagetype = cart" ) )
{
echo $ showRightcolumn? 'contentarea2' : 'newcontentarea';
}
else
echo $showRightColumn ? 'contentarea2' : 'contentarea';

在这个声明中

<div id="<?php echo $showRightColumn ? 'contentarea2' : 'contentarea'; ?>">

最佳答案

对于任何复杂的处理,您始终可以创建一个函数来返回您想要返回的 ID,然后回显该函数的结果,例如:

<?php
  function getId($vm, $showRightColumn) {
    if ($vm->check("pagetype = cart")) {
      return $showRightcolumn ? 'contentarea2' : 'newcontentarea';
    }
    return $showRightColumn ? 'contentarea2' : 'contentarea';
  }
?>
<div id="<?php echo getId($vm, $showRightColumn); ?>">...</div>

但是,对于更简单的条件,最好使用评估其他三元语句的三元运算符(如其他答案所建议的那样)。

关于php - 如何为 div id 设置条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23374434/

相关文章:

jquery - 为不同的背景加载 firefox 特定的 css

css - 绝对定位的 DIV 跨浏览器不一致

php - 在lumen 5.6应用中找不到驱动程序错误(在docker + alpine linux + nginx + php-fpm 7.2中运行)

php - 保持 Zend_Form 不渲染 <form> 标签

html - css 动画状态栏不从 0 加载到它的值

css - html div float 和显示

php - Android - 使用 php 将图像上传到服务器的最佳方法

php - 表格内的下拉菜单 - 然后更新

html - Haskell - Parsec 解析 <p> 元素

css - 在保留样式的同时居中 Bootstrap 导航栏