4

我需要关于 if 语句的帮助,这就是正在发生的事情。

我有一个价值被拉 $paint['product_id']

我需要说这个值是否在 81501 - 81599 或 81701 - 81799 之间

否则,如果该值介于 81001 - 81099 或 81301 - 81399 之间,请说 blah2

else if 86501 - 86599 or 86001 - 86099 or 85001 - 85099 say blah3

如果不适用,什么也不说。

尝试了什么 id

<? if ($paint['product_id'] >= 81501 && $x <= 81599 || $paint['product_id'] >= 81701 && $x <= 81799):?>
blah
<? elseif ($paint['product_id'] >= 81001 && $x <= 81099 || $paint['product_id'] >= 81301 && $x <= 81399):?>
blah2
<? elseif ($paint['product_id'] >= 86501 && $x <= 86599 || $paint['product_id'] >= 86001 && $x <= 86099 || $paint['product_id'] >= 85001 && $x <= 85099):?>
blah3
<? endif;?>

我遇到的问题是“blah”出现在 blah3 类别中的项目上。

希望这是有道理的,并提前感谢您的帮助!

4

7 回答 7

5

替换$x$paint['product_id']

于 2013-06-10T19:45:40.337 回答
4

您应该用括号将它们分组:

if( ($x > 10 && $x < 20) || ($x > 40 && $x < 50) ) { ...
于 2013-06-10T19:46:19.617 回答
2

考虑创建一个用户定义的函数,例如

function between($x, $lim1, $lim2) {
  if ($lim1 < $lim2) {
    $lower = $lim1; $upper = $lim2;
  }
  else {
    $lower = $lim2; $upper = $lim1;
  }
  return (($x >= $lower) && ($x <= $upper));
}

然后你的其余代码变得更加清晰:

if between($paint['product_id'], 81501, 81599) blah;

如给定的那样,即使您不提前知道第一个或第二个参数是否更大,“between”函数也将起作用。

于 2013-06-10T20:04:22.510 回答
1

至少有3种方法可以解决它。

用户已经发布了第一个解决方案

第二种解决方案是在函数之间创建并使用它。

function between($number, $from, $to)
{
   return $number>$from && $number<$to;
}

  if(between($paint['product_id'], 81501, 81599) || $paint['product_id'], 81701, 81799))
   echo 'blah'; 
  else if(between($paint['product_id'], 81001, 81099) || $paint['product_id'], 81301, 81399))
   echo 'blah2';

  else if(between($paint['product_id'], 86501, 86599) || $paint['product_id'], 86001, 86099) || $paint['product_id'], 85001, 85099))
   echo 'blah3';
  else echo 'it does not apply';

第三种解决方案是使用range()函数和in_array()函数

例子if(in_array($paint['product_id'], range(81501, 81599)))

休息一样

于 2013-06-10T19:58:42.193 回答
0

你需要更多的括号

<?php
<? if (($paint['product_id'] >= 81501 && $x <= 81599) || ($paint['product_id'] >= 81701 && $x <= 81799)):?>
blah
<? elseif (($paint['product_id'] >= 81001 && $x <= 81099) || ($paint['product_id'] >= 81301 && $x <= 81399)):?>
blah2
<? elseif (($paint['product_id'] >= 86501 && $x <= 86599) || ($paint['product_id'] >= 86001 && $x <= 86099) || ($paint['product_id'] >= 85001 && $x <= 85099)):?>
blah3
<? endif;?>
于 2013-06-10T19:47:08.767 回答
0
if ($paint['product_id'] >= 81501 && $x <= 81599 || $paint['product_id'] >= 81701         && $x <= 81799){
    echo "blah";
}
elseif ($paint['product_id'] >= 81001 && $x <= 81099 || $paint['product_id'] >= 81301 && $x <= 81399){
    echo "blah2";
}
elseif ($paint['product_id'] >= 86501 && $x <= 86599 || $paint['product_id'] >= 86001 && $x <= 86099 || $paint['product_id'] >= 85001 && $x <= 85099){
    echo "blah2";
}
于 2013-06-10T19:48:19.890 回答
0

您好,我将通过简单的步骤描述如何在 PHP 中的两个数字之间使用 if 条件。

<?php

$a= 65;
$b= 9;

if($a<$b)
    {
        echo  "$a this is correct";
    }

else{
    echo "$b this is greater than $a";
}


?>
于 2021-06-23T04:54:49.723 回答