PHP:如何确定变量的值是否介于两个不同的常量值之间?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5656711/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 22:07:36  来源:igfitidea点击:

PHP: How can I determine if a variable has a value that is between two distinct constant values?

phpif-statementintervalsbetween

提问by Gabriel Meono

How can I determine using PHP code that, for example, I have a variable that has a value

我如何确定使用 PHP 代码,例如,我有一个具有值的变量

  • between 1 and 10, or
  • between 20 and 40?
  • 1 到 10 之间,或
  • 在 20 到 40 之间?

回答by Daniel A. White

if (($value > 1 && $value < 10) || ($value > 20 && $value < 40))

回答by Ryan

Do you mean like:

你的意思是像:

$val1 = rand( 1, 10 ); // gives one integer between 1 and 10
$val2 = rand( 20, 40 ) ; // gives one integer between 20 and 40

or perhaps:

也许:

$range = range( 1, 10 ); // gives array( 1, 2, ..., 10 );
$range2 = range( 20, 40 ); // gives array( 20, 21, ..., 40 );

or maybe:

或者可能:

$truth1 = $val >= 1 && $val <= 10; // true if 1 <= x <= 10
$truth2 = $val >= 20 && $val <= 40; // true if 20 <= x <= 40

suppose you wanted:

假设你想要:

$in_range = ( $val > 1 && $val < 10 ) || ( $val > 20 && $val < 40 ); // true if 1 < x < 10 OR 20 < x < 40

回答by Heddie Franco

You can do this:

你可以这样做:

if(in_array($value, range(1, 10)) || in_array($value, range(20, 40))) {
   # enter code here
}

回答by don

if (($value >= 1 && $value <= 10) || ($value >= 20 && $value <= 40)) {
   // A value between 1 to 10, or 20 to 40.
}

回答by Codecraft

Guessing from the tag 'operand' you want to check a value?

从标签“操作数”中猜测您想检查一个值吗?

$myValue = 5;
$minValue = 1;
$maxValue = 10;

if ($myValue >= $minValue && $myValue <= $maxValue) { 
  //do something
}

回答by Aurangzeb

returns true if subject is between low and high (inclusive)

如果主题介于低和高(包括)之间,则返回 true

$between = function( $low, $high, $subject ) {
    if( $subject < $low ) return false;
    if( $subject > $high ) return false;
    return true;
};

if( $between( 0, 100, $givenNumber )) {
   // do whatever...
}

looks cleaner to me

对我来说看起来更干净

回答by Peter Berwanger

If you just want to check the value is in Range, use this:

如果您只想检查值是否在范围内,请使用以下命令:

   MIN_VALUE = 1;
   MAX_VALUE = 100;
   $customValue = min(MAX_VALUE,max(MIN_VALUE,$customValue)));

回答by kakarot

Try This

尝试这个

if (($val >= 1 && $val <= 10) || ($val >= 20 && $val <= 40))

This will return the value between 1 to 10 & 20 to 40.

回答by ohmusama

A random value?

一个随机值?

If you want a random value, try

如果你想要一个随机值,试试

<?php
$value = mt_rand($min, $max);

mt_rand() will run a bit more random if you are using many random numbers in a row, or if you might ever execute the script more than once a second. In general, you should use mt_rand() over rand() if there is any doubt.

如果您连续使用许多随机数,或者您可能每秒多次执行脚本,则 mt_rand() 将运行得更加随机。一般来说,如果有任何疑问,您应该使用 mt_rand() 而不是 rand()。