user3762572
user3762572

Reputation: 25

multiple or operator conditions in if statement

i want to show some text if condition is true like this

    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 = width $h = height

Please tell me how to correct above condition

Upvotes: 0

Views: 100

Answers (4)

Redoneben
Redoneben

Reputation: 126

Or you can do this:

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

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

Upvotes: 0

Jijo John
Jijo John

Reputation: 1375

It's simple use && for AND and || for OR. see the PHP Logical operators documentation for understanding php logical operators in depth..

 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   
    }

Upvotes: 0

Dr.Tricker
Dr.Tricker

Reputation: 553

<?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;
}

?>

Upvotes: 0

manjeet
manjeet

Reputation: 1517

you can do something like this, will be a bit slow, but works

<?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"; 
}

Upvotes: 1

Related Questions