PHP“if”语句在两个数字之间寻找

时间:2013-06-10 19:42:16

标签: php if-statement

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

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

我需要说明这个值是否介于两者之间 81501 - 81599或 81701 - 81799 说布拉什

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

否则如果 86501 - 86599或 86001 - 86099或 85001 - 85099 说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类别的项目中。

希望有意义,并提前感谢任何帮助!

7 个答案:

答案 0 :(得分:5)

将$ x替换为$ paint [&#39; product_id&#39;]。

答案 1 :(得分:4)

您应该用括号对它们进行分组:

if( ($x > 10 && $x < 20) || ($x > 40 && $x < 50) ) { ...

答案 2 :(得分: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)))

休息是一样的

答案 3 :(得分:1)

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

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”函数也会起作用。

答案 4 :(得分: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;?>

答案 5 :(得分:0)

if($ paint ['product_id']&gt; = 81501&amp;&amp; $ x&lt; = 81599 || $ paint ['product_id']&gt; = 81701&amp;&amp; $ x&lt; = 81799) {
回声“blah”;
}
elseif($ paint ['product_id']&gt; = 81001&amp;&amp; $ x&lt; = 81099 || $ paint ['product_id']&gt; = 81301&amp;&amp; $ x&lt; = 81399){
回声“blah2”;
}
elseif($ paint ['product_id']&gt; = 86501&amp;&amp; $ x&lt; = 86599 || $ paint ['product_id']&gt; = 86001&amp;&amp; $ x&lt; = 86099 || $ paint ['product_id']&gt; = 85001&amp;&amp; $ x&lt; = 85099){
回声“blah2”;
}

答案 6 :(得分:0)

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

<?php

$a= 65;
$b= 9;

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

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


?>