<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Find lowest value</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.max.html">max</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.mt-getrandmax.html">mt_getrandmax</a></div> <div class="up"><a href="ref.math.html">Math Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="function.min" class="refentry"> <div class="refnamediv"> <h1 class="refname">min</h1> <p class="verinfo">(PHP 4, PHP 5, PHP 7)</p><p class="refpurpose"><span class="refname">min</span> — <span class="dc-title">Find lowest value</span></p> </div> <div class="refsect1 description" id="refsect1-function.min-description"> <h3 class="title">Description</h3> <div class="methodsynopsis dc-description"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>min</strong></span> ( <span class="methodparam"><span class="type">array</span> <code class="parameter">$values</code></span> )</div> <div class="methodsynopsis dc-description"> <span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <span class="methodname"><strong>min</strong></span> ( <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value1</code></span> , <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$value2</code></span> [, <span class="methodparam"><span class="type"><a href="language.pseudo-types.html#language.types.mixed" class="type mixed">mixed</a></span> <code class="parameter">$...</code></span> ] )</div> <p class="para rdfs-comment"> If the first and only parameter is an array, <span class="function"><strong>min()</strong></span> returns the lowest value in that array. If at least two parameters are provided, <span class="function"><strong>min()</strong></span> returns the smallest of these values. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> Values of different types will be compared using the <a href="language.operators.comparison.html" class="link"> standard comparison rules</a>. For instance, a non-numeric <span class="type"><a href="language.types.string.html" class="type string">string</a></span> will be compared to an <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> as though it were <em>0</em>, but multiple non-numeric <span class="type"><a href="language.types.string.html" class="type string">string</a></span> values will be compared alphanumerically. The actual value returned will be of the original type with no conversion applied. </p> </p></blockquote> <div class="caution"><strong class="caution">Caution</strong> <p class="simpara"> Be careful when passing arguments with mixed types values because <span class="function"><strong>min()</strong></span> can produce unpredictable results. </p> </div> </div> <div class="refsect1 parameters" id="refsect1-function.min-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <code class="parameter">values</code></dt> <dd> <p class="para"> An array containing the values. </p> </dd> <dt> <code class="parameter">value1</code></dt> <dd> <p class="para"> Any <a href="language.operators.comparison.html" class="link">comparable</a> value. </p> </dd> <dt> <code class="parameter">value2</code></dt> <dd> <p class="para"> Any <a href="language.operators.comparison.html" class="link">comparable</a> value. </p> </dd> <dt> <code class="parameter">...</code></dt> <dd> <p class="para"> Any <a href="language.operators.comparison.html" class="link">comparable</a> value. </p> </dd> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.min-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> <span class="function"><strong>min()</strong></span> returns the parameter value considered "lowest" according to standard comparisons. If multiple values of different types evaluate as equal (e.g. <em>0</em> and <em>'abc'</em>) the first provided to the function will be returned. </p> <p class="para"> If an empty array is passed, then <strong><code>FALSE</code></strong> will be returned and an <strong><code>E_WARNING</code></strong> error will be emitted. </p> </div> <div class="refsect1 examples" id="refsect1-function.min-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-4261"> <p><strong>Example #1 Example uses of <span class="function"><strong>min()</strong></span></strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">min</span><span style="color: #007700">(</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">6</span><span style="color: #007700">, </span><span style="color: #0000BB">7</span><span style="color: #007700">); </span><span style="color: #FF8000">// 1<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">min</span><span style="color: #007700">(array(</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">)); </span><span style="color: #FF8000">// 2<br /><br />// The string 'hello' when compared to an int is treated as 0<br />// Since the two values are equal, the order they are provided determines the result<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">min</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #DD0000">'hello'</span><span style="color: #007700">); </span><span style="color: #FF8000">// 0<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">min</span><span style="color: #007700">(</span><span style="color: #DD0000">'hello'</span><span style="color: #007700">, </span><span style="color: #0000BB">0</span><span style="color: #007700">); </span><span style="color: #FF8000">// hello<br /><br />// Here we are comparing -1 < 0, so -1 is the lowest value<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">min</span><span style="color: #007700">(</span><span style="color: #DD0000">'hello'</span><span style="color: #007700">, -</span><span style="color: #0000BB">1</span><span style="color: #007700">); </span><span style="color: #FF8000">// -1<br /><br />// With multiple arrays of different lengths, min returns the shortest<br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= </span><span style="color: #0000BB">min</span><span style="color: #007700">(array(</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">2</span><span style="color: #007700">), array(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">)); </span><span style="color: #FF8000">// array(2, 2, 2)<br /><br />// Multiple arrays of the same length are compared from left to right<br />// so in our example: 2 == 2, but 4 < 5<br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= </span><span style="color: #0000BB">min</span><span style="color: #007700">(array(</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">4</span><span style="color: #007700">, </span><span style="color: #0000BB">8</span><span style="color: #007700">), array(</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">, </span><span style="color: #0000BB">1</span><span style="color: #007700">)); </span><span style="color: #FF8000">// array(2, 4, 8)<br /><br />// If both an array and non-array are given, the array is never returned<br />// as comparisons treat arrays as greater than any other value<br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= </span><span style="color: #0000BB">min</span><span style="color: #007700">(</span><span style="color: #DD0000">'string'</span><span style="color: #007700">, array(</span><span style="color: #0000BB">2</span><span style="color: #007700">, </span><span style="color: #0000BB">5</span><span style="color: #007700">, </span><span style="color: #0000BB">7</span><span style="color: #007700">), </span><span style="color: #0000BB">42</span><span style="color: #007700">); </span><span style="color: #FF8000">// string<br /><br />// If one argument is NULL or a boolean, it will be compared against<br />// other values using the rules FALSE < TRUE and NULL == FALSE regardless of the <br />// other types involved<br />// In the below examples, both -10 and 10 are treated as TRUE in the comparison<br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= </span><span style="color: #0000BB">min</span><span style="color: #007700">(-</span><span style="color: #0000BB">10</span><span style="color: #007700">, </span><span style="color: #0000BB">FALSE</span><span style="color: #007700">, </span><span style="color: #0000BB">10</span><span style="color: #007700">); </span><span style="color: #FF8000">// FALSE<br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= </span><span style="color: #0000BB">min</span><span style="color: #007700">(-</span><span style="color: #0000BB">10</span><span style="color: #007700">, </span><span style="color: #0000BB">NULL</span><span style="color: #007700">, </span><span style="color: #0000BB">10</span><span style="color: #007700">); </span><span style="color: #FF8000">// NULL<br /><br />// 0, on the other hand, is treated as FALSE, so is "lower than" TRUE<br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= </span><span style="color: #0000BB">min</span><span style="color: #007700">(</span><span style="color: #0000BB">0</span><span style="color: #007700">, </span><span style="color: #0000BB">TRUE</span><span style="color: #007700">); </span><span style="color: #FF8000">// 0<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.min-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"><span class="function"><a href="function.max.html" class="function" rel="rdfs-seeAlso">max()</a> - Find highest value</span></li> <li class="member"><span class="function"><a href="function.count.html" class="function" rel="rdfs-seeAlso">count()</a> - Count all elements in an array, or something in an object</span></li> </ul> </p> </div> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.max.html">max</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.mt-getrandmax.html">mt_getrandmax</a></div> <div class="up"><a href="ref.math.html">Math Functions</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>