Web студия "GrandView"
  Главная   Написать Контакты
   
   
О проекте
Руководство php
 

min

(PHP 3, PHP 4, PHP 5)

min -- Находит наименьшее значение

Описание

mixed min ( number arg1, number arg2 [, number ...] )

mixed min ( array numbers )

Возвращает наименьшее из предложенных чисел.

Если в качестве аргументов передан только один - массив чисел, min() возвращает наименьшее из них. Если первый аргумент - integer или float, то обязательно должен быть хотя бы ещё один. В этом случае функция вернёт наименьшее из них. Можно сравнивать неограниченное количество чисел.

Замечание: PHP считает строку, которую не удалось преобразовать в число, как 0, но вернёт саму строку, в случае, если все остальные числа положительные. Если указано несколько элементов, эквивалентных 0, min() вернёт первый из них (самый левый).

Пример 1. Примеры использования min()

<?php
echo min(2, 3, 1, 6, 7);  // 1
echo min(array(2, 4, 5)); // 2

echo min(0, 'hello');     // 0
echo min('hello', 0);     // hello
echo min('hello', -1);    // -1

// При указании нескольких массивов, они сравниваются полностью
// в нашем примере: 2 == 2, но 4 < 5
$val = min(array(2, 4, 8), array(2, 5, 1)); // array(2, 4, 8)

// При указании массива и отдельных чисел,
// первый никогда не возвращается, т.к. считается самым большим
$val = min('string', array(2, 5, 7), 42);   // string
?>

См также max() и count().



mt_getrandmax> <max
Last updated: Sat, 27 Jan 2007
 
add a note add a note User Contributed Notes
min
dave at dtracorp dot com
13-Aug-2006 08:30
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 < $minVal) {
$minVal = $value;
}
johnphayes at gmail dot com
02-May-2006 09:26
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
steffen at morkland dot com
16-Mar-2006 02:16
> 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 ===
31-Jan-2006 09:37
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 !
DASPRiD
Новости
11 июля 2007
Сайт запущен
© 2007 info@grandviewstudio.com

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 324

Deprecated: Function set_magic_quotes_runtime() is deprecated in /home/sites/grandviewstudiocom/www/65f67d67a94ad980786580ae69e11c07/sape.php on line 330
Z058440144362 Z348613067571