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