if语句中的多个或运算符条件

时间:2015-08-23 09:22:14

标签: php if-statement

如果条件为真,我想显示一些文字

    if ($imageurl=="" OR ($w != "1920" AND $h != "1080") OR ($w != "1440" AND $h != "900") OR ($w != "1366" AND $h != "768") OR ($w != "1280" AND $h != "1024") OR ($w != "1280" AND $h != "800") OR ($w != "1024" AND $h != "768") OR ($w != "800" AND $h != "600") OR ($w != "360" AND $h != "640")) 

{
   text to print   
}

$ w =宽度 $ h =身高

请告诉我如何纠正上述情况

4 个答案:

答案 0 :(得分:1)

你可以做这样的事情,会有点慢,但有效

<?php

$h = array(
          '1920',
          '1440',
          '1366',
          '1280',
          '1280',
          '1024',
          "800",
          "360"
          );

$w = array(
           '1080',
           "900",
           "768",
           "1024",
           "800",
           "768",
           "600",
           "640"
           );

$width = 125;
$height = 125;

if($imageurl == "" || (!in_array($width, $w) && !in_array($height, $h)) )
{
   echo "condition satisfied"; 
}

答案 1 :(得分:0)

<?php 

$w = 1920;
$h = 452;

$arr[1920] = 1080;
$arr[1440] = 900;
$arr[1366] = 768;
$arr[1280] = 1024;
$arr[1280] = 800;
$arr[1024] = 768;
$arr[800] = 600;
$arr[360] = 640;

if($imageurl != "" || (array_key_exists($w, $arr) && in_array($h, $arr))){
    echo 'Accepted size='.$w.'x'.$h;
}else{
    echo 'Not Accepted size='.$w.'x'.$h;
}

?>

答案 2 :(得分:0)

简单地使用&&表示AND,||表示OR。请参阅 PHP Logical operators文档以深入了解php逻辑运算符。

 if (($imageurl=="") || ($w != "1920" && $h != "1080") || ($w != "1440" && $h != "900") || ($w != "1366" && $h != "768") || ($w != "1280" && $h != "1024") || ($w != "1280" && $h != "800") || ($w != "1024" && $h != "768") || ($w != "800" && $h != "600") || ($w != "360" && $h != "640")) 

    {
       text to print   
    }

答案 3 :(得分:0)

或者你可以这样做:

$sizes = array (
          '1920x1080',
          '1440x900',
          '1366x768',
          '1280x1024',
          '1280x800',
          '1024x768',
          '800x600',
          '360x640');

if (empty($imageurl) || !in_array($w.'x'.$h, $sizes, true))
{
  echo 'error';
}