如果不在 PHP 中,如何使用?

How to work with if not in PHP?

我想打印 div 和 class 调用 inner-content-div,如果变量 $name 不包含以下字符串。

但是我的 PHP 代码不起作用。

这是我的代码。

<?php
  $vid = explode("/", $_GET["q"]);
  $name = taxonomy_term_load($vid[2]);
?>

<h1 id="page-title" class="title"><?php print $name->name; ?></h1>

<?php 
  $exclude_list = array("Gingelly Rolls","Kithul Treacle","Coconut Vinegar");
  if(!in_array($name, $exclude_list)){       ?>
    <div class="inner-content-div">
       <!-- Here are some HTML code.-->
    </div>
<?php 
  } 
?>

应该是 -

if(!in_array($name->name, $exclude_list)){

打印时 -

<h1 id="page-title" class="title"><?php print $name->name; ?></h1>
<?php
  $vid = explode("/", $_GET["q"]);
  $name = taxonomy_term_load($vid[2]);
  $exclude_list = array("Gingelly Rolls","Kithul Treacle","Coconut Vinegar");
?>

<h1 id="page-title" class="title"><?php echo $name->name; ?></h1>

<?php if(!in_array($name->name, $exclude_list)): ?>
    <div class="inner-content-div">
       <!-- Here are some HTML code.-->
    </div>
<?php endif; ?>

可能是因为 $name 是一个对象。此外,可能更好地使用 PHP 的替代语法来清洁 html/templates.

此处的另一个潜在问题是排除列表中项目的大小写可能与名称的值不匹配。在进行比较时,您可能应该将它们标准化为所有小写字母。

你可以这样试试。首先使用 echo $vid[2]; 打印 $vid[2]。然后将 $vid[2] 个值放入名为 $exclude_list 的数组中。然后使用下面的代码。

$exclude_list = array(//value 1, value 2, value 3);
if(!in_array($vid[2], $exclude_list)) {
   //Your HTML code
}