PHP“if”语句在两个数字之间寻找
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17031541/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
PHP "if" statement looking for between two numbers
提问by user2465180
I need help on a if statement here is what is going on.
我需要关于 if 语句的帮助,这里是发生了什么。
I have a value being pulled $paint['product_id']
我有一个价值被拉 $paint['product_id']
I need to say if that value is between 81501 - 81599 or 81701 - 81799 say blah
我需要说这个值是否在 81501 - 81599 或 81701 - 81799 之间,等等
else if that value is between 81001 - 81099 or 81301 - 81399 say blah2
否则,如果该值介于 81001 - 81099 或 81301 - 81399 之间,则说 blah2
else if 86501 - 86599 or 86001 - 86099 or 85001 - 85099 say blah3
否则如果 86501 - 86599 或 86001 - 86099 或 85001 - 85099 说 blah3
and say nothing if it does not apply.
如果不适用,则什么也不说。
What id did try
什么 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;?>
The problem I am having is "blah" is showing up on items in the blah3 category.
我遇到的问题是“blah”出现在 blah3 类别中的项目上。
Hope that makes sense and thanks in advance for any help!
希望这是有道理的,并在此先感谢您的帮助!
回答by mzedeler
Replace $x
with $paint['product_id']
.
替换$x
为$paint['product_id']
。
回答by Royal Bg
You should group them with brackets:
你应该用括号将它们分组:
if( ($x > 10 && $x < 20) || ($x > 40 && $x < 50) ) { ...
回答by Floris
Consider creating a user defined function, e.g.
考虑创建一个用户定义的函数,例如
function between($x, $lim1, $lim2) {
if ($lim1 < $lim2) {
$lower = $lim1; $upper = $lim2;
}
else {
$lower = $lim2; $upper = $lim1;
}
return (($x >= $lower) && ($x <= $upper));
}
Then the rest of your code becomes much more legible:
然后你的其余代码变得更加清晰:
if between($paint['product_id'], 81501, 81599) blah;
As given, the "between" function will work even if you don't know ahead of time whether the first or the second argument is larger.
正如给定的,即使您不知道第一个或第二个参数是否更大,“between”函数也会起作用。
回答by Robert
there are at least 3 ways to solve it.
至少有 3 种方法可以解决它。
First solution has been already posted by users
用户已经发布了第一个解决方案
Second solution is to create between function and use it.
第二种解决方案是在函数之间创建并使用它。
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';
Third solution is to use range()
function and in_array()
function
第三种解决方案是使用range()
函数和in_array()
函数
example if(in_array($paint['product_id'], range(81501, 81599)))
例子 if(in_array($paint['product_id'], range(81501, 81599)))
rest goes the same
休息是一样的
回答by Orangepill
You need more parenthesis
你需要更多的括号
<?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;?>
回答by Charlton Santana
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";
}