百科狗-知识改变命运!
--

min() - 找出最小值 - php 数学函数

乐乐1年前 (2023-11-21)阅读数 53#技术干货
文章标签数组

min()

(PHP 4, PHP 5, PHP 7)

找出最小值

说明

min(array $values): mixedmin(mixed $value1,mixed $value2[,mixed$...]): mixed

如果仅有一个参数且为数组,min()返回该数组中最小的值。如果给出了两个或更多参数,min()会返回这些值中最小的一个。

Note:

PHP 会将非数值的string当成0,但如果这个正是最小的数值则仍然会返回一个字符串。如果多个参数都求值为0且是最小值,min()会返回按字母表顺序最小的字符串,如果其中没有字符串的话,则返回数值的0

参数

$values

包含值的数组。

$value1

Anycomparablevalue.

$value2

Anycomparablevalue.

...

min() - 找出最小值 - php 数学函数

Anycomparablevalue.

返回值

min()返回参数中数值最小的。

范例

Example #1min()用法的例子

参见

  • max() 找出最大值
  • count() 计算数组中的单元数目,或对象中的属性个数
min() (and max()) on DateTime objects compares them like dates (with timezone info) and returns DateTime object.

It works at least 5.3.3-7+squeeze17
A function that returns the lowest integer that is not 0. 
A way to bound a integer between two values is:
function bound($x, $min, $max)
{
   return min(max($x, $min), $max);
}
which is the same as:
$tmp = $x;
if($tmp  $max)
{
   $tmp = $max;
}
$y = $tmp;
So if you wanted to bound an integer between 1 and 12 for example:
Input:
$x = 0;
echo bound(0, 1, 12).'
'; $x = 1; echo bound($x, 1, 12).'
'; $x = 6; echo bound($x, 1, 12).'
'; $x = 12; echo bound($x, 1, 12).'
'; $x = 13; echo bound($x, 1, 12).'
'; Output: 1 1 6 12 12
A min_by function: 
NEVER EVER use this function with boolean variables !!!
Or you'll get something like this: min(true, 1, -2) == true;
Just because of:
min(true, 1, -2) == min(min(true,1), -2) == min(true, -2) == true;
You are warned !
Regarding boolean parameters in min() and max():
(a) If any of your parameters is boolean, max and min will cast the rest of them to boolean to do the comparison.
(b) true > false
(c) However, max and min will return the actual parameter value that wins the comparison (not the cast).
Here's some test cases to illustrate:
1. max(true,100)=true
2. max(true,0)=true
3. max(100,true)=100
4. max(false,100)=100
5. max(100,false)=100
6. min(true,100)=true
7. min(true,0)=0
8. min(100,true)=100
9. min(false,100)=false
10. min(100,false)=false
11. min(true,false)=false
12. max(true,false)=true
Here is function can find min by array key 
Be very careful when your array contains both strings and numbers. This code works strange (even though explainable) way:
var_dump(max('25.1.1', '222', '99'));
var_dump(max('2.1.1', '222', '99'));
> NEVER EVER use this function with boolean variables !!!
> Or you'll get something like this: min(true, 1, -2) == true;
> Just because of:
> min(true, 1, -2) == min(min(true,1), -2) == min(true, -2) == true;
It is possible to use it with booleans, there is is just one thing, which you need to keep in mind, when evaluating using the non strict comparison (==) anyting that is not bool false, 0 or NULL is consideret true eg.:
(5 == true) = true;
(0 == true) = false;
true is also actually anything else then 0, false and null. However when true is converted to a string or interger true == 1, therefore when sorting true = 1. But if true is the maximum number bool true is returned. so to be sure, if you only want to match if true is the max number remember to use the strict comparison operater ===
I've modified the bugfree min-version to ignore NULL values (else it returns 0). 
If you want min to return zero (0) when comparing to a string, try this: 
I tested this with max(), but I suppose it applies to min() too: If you are working with numbers, then you can use:
 
  $a = ($b 
If NAN is the first argument to min(), the second argument will always be returned.
If NAN is the second argument, NAN will always be returned.
The relationship is the same but inverted for max(). 
min() can be used to cap values at a specific value. For instance, if you're grading papers and someone has some extra credit, but that shouldn't make it to the final score:
$pts_possible = 50;
$score = 55;
// Percent will equal 1 if $score/$pts_possible is greater than 1
$percent = min($score/$pts_possible,1);
empty strings '' will also return false or 0, so if you have something like
$test = array('', 1, 5, 8, 44, 22);
'' will be returned as the lowest value
if you only want to get the lowest number, you'll have to resort to the old fashioned loop
// default minimum value
$minVal = 100;
foreach ($test as $value) {
if (is_numeric($value) && $value 

鹏仔微信 15129739599 鹏仔QQ344225443 鹏仔前端 pjxi.com 共享博客 sharedbk.com

免责声明:我们致力于保护作者版权,注重分享,当前被刊用文章因无法核实真实出处,未能及时与作者取得联系,或有版权异议的,请联系管理员,我们会立即处理! 部分文章是来自自研大数据AI进行生成,内容摘自(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供学习参考,不准确地方联系删除处理!邮箱:344225443@qq.com)

图片声明:本站部分配图来自网络。本站只作为美观性配图使用,无任何非法侵犯第三方意图,一切解释权归图片著作权方,本站不承担任何责任。如有恶意碰瓷者,必当奉陪到底严惩不贷!

内容声明:本文中引用的各种信息及资料(包括但不限于文字、数据、图表及超链接等)均来源于该信息及资料的相关主体(包括但不限于公司、媒体、协会等机构)的官方网站或公开发表的信息。部分内容参考包括:(百度百科,百度知道,头条百科,中国民法典,刑法,牛津词典,新华词典,汉语词典,国家院校,科普平台)等数据,内容仅供参考使用,不准确地方联系删除处理!本站为非盈利性质站点,本着为中国教育事业出一份力,发布内容不收取任何费用也不接任何广告!)