开发者

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. How do I add an OR function to this?


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:

if(in_array($pageid, array(9, 5, 3, 12, 5)) { echo ...

or works instead of || 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.

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


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. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜