这可能是一个简单的答案,但我被困在我的一个项目中
如果我的集合或函数很少,我想显示文本,很抱歉我的问题,我不确定如何调用它们。
这就是我的意思
<?php if($doc->type == "mp3"): ?>
<p> text and some other html here </p>
<?php endif; ?>我想在这里找到一些"dic类型“,并展示相同的我可以一个接一个地做,但这将是加载的代码,我认为它可能是一些解决方案,如:
<?php if($doc->type == "mp3", "mp4", "doc" etc....): ?>
<p> text and some other html here </p>
<?php endif; ?>我想在几种类型中选择,并显示相同的文本,我现在的解决方案是逐个添加它们
<?php if($doc->type == "mp3"): ?>
<p> text and some other html here </p>
<?php endif; ?>
<?php if($doc->type == "mp4"): ?>
<p> text and some other html here </p>
<?php endif; ?>
<?php if($doc->type == "doc"): ?>
<p> text and some other html here </p>
<?php endif; ?>如果有任何方法可以做到这一点,那就太好了,真的很感激
谢谢
发布于 2016-12-05 22:52:32
你有多种方法可以做到这一点。一种更简单的方法是使用in_array
<?php if(in_array($doc->type, ['doc', 'mp3', 'mp4'])): ?>
<p> text and some other html here </p>
<?php endif; ?>发布于 2016-12-05 22:52:44
使用开关柜:
手册:http://php.net/manual/en/control-structures.switch.php
示例:
<?php
switch ($i) {
case "apple":
echo "i is apple";
break;
case "bar":
echo "i is bar";
break;
case "cake":
echo "i is cake";
break;
}
?>发布于 2016-12-05 23:01:25
如果使用logical operators (||表示'OR',&&表示'AND')在同一个表达式中组合几个条件呢?
<?php if($doc->type == "mp3" || $doc->type == "mp4"): ?>
<p> text and some other html here </p>
<?php endif; ?>https://stackoverflow.com/questions/40976966
复制相似问题