[英]How do I add an OR function to a PHP code?
Firstly, dont laugh, I know it is basic. 首先,别笑,我知道这很基本。
I have this code: if ($pageid == '9') { echo...
and I want to add an OR to it, so I tried: if ($pageid == '9' or '5') { echo...
but it didnt work. 我有这段代码:
if ($pageid == '9') { echo...
,我想为其添加一个OR,所以我尝试了: if ($pageid == '9' or '5') { echo...
但是没用。 How do I add an OR function to this? 如何为此添加OR函数?
if ($pageid == '9' || $pageid == '5') { echo...
Note that if you have a large number of pageids to check for some reason, you can utilize arrays: 请注意,如果出于某些原因要检查大量的pageid,则可以利用数组:
if(in_array($pageid, array(9, 5, 3, 12, 5)) { echo ...
or
works instead of ||
or
代替||
起作用 also but ||
也
||
is more commonly used. 更常用。
Just to mention another approach that makes use of PHP's in_array - it's quite handy if you've a few values you want to treat as a cluster that don't justify a switch but would be painfully verbose to use a traditional or for. 只需提及另一种利用PHP的in_array的方法 -如果您想将一些值视为一个群集,这些值不能证明切换的合理性,但使用传统的或冗长的名称将非常麻烦 ,这非常方便。
if(in_array($pageID, array(1, 2, 3))) {
// Do exciting things...
}
if ($pageid == '9' || $pageid == '5')
this should work too. 这也应该工作。 i am not sure why this won't work in your case
我不确定为什么这在您的情况下不起作用
if($pageid == "9" OR $pageid == "5"){
echo "pageid ".$pageid;
}
for more information see this http://php.net/manual/en/language.operators.logical.php 有关更多信息,请参见http://php.net/manual/en/language.operators.ologic.php
switch ($pageid) {
case '5':
case '9':
echo "...";
break;
default:
// Do something useful
break;
}
This is useful, if you have more than one cases, because it prevents you from ugly "elseif"-chains. 如果您有不止一种情况,这很有用,因为它可以防止您使用难看的“ elseif”链。 Also if you have one case with many different values its more readable and its easy to add additional values.
同样,如果您有一个具有许多不同值的案例,那么它的可读性就更高,并且易于添加其他值。 You just need to add a new
case 'xy':
line. 您只需要添加新的
case 'xy':
行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.