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

发布于 2024-11-01 03:54:09 字数 91 浏览 4 评论 0原文

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

  • 介于 1 和 10 之间或
  • 介于 20 和 40 之间的变量?

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

  • between 1 and 10, or
  • between 20 and 40?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

离线来电— 2024-11-08 03:54:09
if (($value > 1 && $value < 10) || ($value > 20 && $value < 40))
if (($value > 1 && $value < 10) || ($value > 20 && $value < 40))
横笛休吹塞上声 2024-11-08 03:54:09

你的意思是像:

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

或者也许:

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

或者也许:

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

假设你想要:

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

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
身边 2024-11-08 03:54:09
if (($value >= 1 && $value <= 10) || ($value >= 20 && $value <= 40)) {
   // A value between 1 to 10, or 20 to 40.
}
if (($value >= 1 && $value <= 10) || ($value >= 20 && $value <= 40)) {
   // A value between 1 to 10, or 20 to 40.
}
倾`听者〃 2024-11-08 03:54:09

很抱歉回答晚了,但是这个功能可以让你做到这一点。

  function int_between($value, $start, $end) {
    return in_array($value, range($start, $end));
  }

  // Example
  $value1 = 20;
  $value2 = 40;
  echo int_between(20, $value1, $value2) ? "true" : "false";

Sorry for the late answer, but this function allow you to do that.

  function int_between($value, $start, $end) {
    return in_array($value, range($start, $end));
  }

  // Example
  $value1 = 20;
  $value2 = 40;
  echo int_between(20, $value1, $value2) ? "true" : "false";
凹づ凸ル 2024-11-08 03:54:09

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

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

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

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

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

if ($myValue >= $minValue && $myValue <= $maxValue) { 
  //do something
}
末蓝 2024-11-08 03:54:09

这是一个很好的解决方案。

function int_between($value, $min, $max) {
    return in_array($value, range($min, $max));
}

我认为这个比上面的快

function int_between2($value, $min, $max) {
    return $value == min($max, max($min, $value));
}

但是,这比其他的快3倍。

function int_between3($value, $min, $max) {
    return ($value >= $min && $value <= $max);
}

This is a good solution.

function int_between($value, $min, $max) {
    return in_array($value, range($min, $max));
}

I think this one is faster than the above

function int_between2($value, $min, $max) {
    return $value == min($max, max($min, $value));
}

But, this is 3 times faster than others.

function int_between3($value, $min, $max) {
    return ($value >= $min && $value <= $max);
}
傾城如夢未必闌珊 2024-11-08 03:54:09

试试这个

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

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

Try This

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

This will return the value between 1 to 10 & 20 to 40.
末が日狂欢 2024-11-08 03:54:09

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

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

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

   MIN_VALUE = 1;
   MAX_VALUE = 100;
   $customValue = 50
   $result = min(MAX_VALUE,max(MIN_VALUE,$customValue))) == $customValue;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文