<!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 highest value</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="function.log.html">log</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.min.html">min</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.max" class="refentry"> <div class="refnamediv"> <h1 class="refname">max</h1> <p class="verinfo">(PHP 4, PHP 5)</p><p class="refpurpose"><span class="refname">max</span> — <span class="dc-title">Find highest value</span></p> </div> <div class="refsect1 description" id="refsect1-function.max-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>max</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>max</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>max()</strong></span> returns the highest value in that array. If at least two parameters are provided, <span class="function"><strong>max()</strong></span> returns the biggest of these values. </p> <blockquote class="note"><p><strong class="note">Note</strong>: <p class="para"> PHP will evaluate a non-numeric <span class="type"><a href="language.types.string.html" class="type string">string</a></span> as <em>0</em> if compared to <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span>, but still return the string if it's seen as the numerically highest value. If multiple arguments evaluate to <em>0</em>, <span class="function"><strong>max()</strong></span> will return a numeric <em>0</em> if given, else the alphabetical highest string value will be returned. </p> </p></blockquote> </div> <div class="refsect1 parameters" id="refsect1-function.max-parameters"> <h3 class="title">Parameters</h3> <p class="para"> <dl> <dt> <span class="term"><em><code class="parameter">values</code></em></span> <dd> <p class="para"> An array containing the values. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">value1</code></em></span> <dd> <p class="para"> Any <a href="language.operators.comparison.html" class="link">comparable</a> value. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">value2</code></em></span> <dd> <p class="para"> Any <a href="language.operators.comparison.html" class="link">comparable</a> value. </p> </dd> </dt> <dt> <span class="term"><em><code class="parameter">...</code></em></span> <dd> <p class="para"> Any <a href="language.operators.comparison.html" class="link">comparable</a> value. </p> </dd> </dt> </dl> </p> </div> <div class="refsect1 returnvalues" id="refsect1-function.max-returnvalues"> <h3 class="title">Return Values</h3> <p class="para"> <span class="function"><strong>max()</strong></span> returns the numerically highest of the parameter values. If multiple values can be considered of the same size, the one that is listed first will be returned. </p> <p class="para"> When <span class="function"><strong>max()</strong></span> is given multiple <span class="type"><a href="language.types.array.html" class="type array">array</a></span>s, the longest array is returned. If all the arrays have the same length, <span class="function"><strong>max()</strong></span> will use lexicographic ordering to find the return value. </p> <p class="para"> When given a <span class="type"><a href="language.types.string.html" class="type string">string</a></span> it will be cast as an <span class="type"><a href="language.types.integer.html" class="type integer">integer</a></span> when comparing. </p> </div> <div class="refsect1 examples" id="refsect1-function.max-examples"> <h3 class="title">Examples</h3> <p class="para"> <div class="example" id="example-3463"> <p><strong>Example #1 Example uses of <span class="function"><strong>max()</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">max</span><span style="color: #007700">(</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">, </span><span style="color: #0000BB">5</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">// 7<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">max</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">// 5<br /><br />// When 'hello' is cast as integer it will be 0. Both the parameters are equally<br />// long, so the order they are given in determines the result<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">max</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">max</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 /></span><span style="color: #007700">echo </span><span style="color: #0000BB">max</span><span style="color: #007700">(</span><span style="color: #DD0000">'42'</span><span style="color: #007700">, </span><span style="color: #0000BB">3</span><span style="color: #007700">); </span><span style="color: #FF8000">// '42'<br /><br />// Here 0 > -1, so 'hello' is the return value.<br /></span><span style="color: #007700">echo </span><span style="color: #0000BB">max</span><span style="color: #007700">(-</span><span style="color: #0000BB">1</span><span style="color: #007700">, </span><span style="color: #DD0000">'hello'</span><span style="color: #007700">); </span><span style="color: #FF8000">// hello<br /><br />// With multiple arrays of different lengths, max returns the longest<br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= </span><span style="color: #0000BB">max</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(1, 1, 1, 1)<br /><br />// With multiple arrays of the same length, max compares from left to right<br />// using lexicographic order, 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">max</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">7</span><span style="color: #007700">)); </span><span style="color: #FF8000">// array(2, 5, 7)<br /><br />// If both an array and non-array are given, the array<br />// is always returned as it's seen as the largest<br /></span><span style="color: #0000BB">$val </span><span style="color: #007700">= </span><span style="color: #0000BB">max</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">// array(2, 5, 7)<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> </div> <div class="refsect1 seealso" id="refsect1-function.max-seealso"> <h3 class="title">See Also</h3> <p class="para"> <ul class="simplelist"> <li class="member"> <span class="function"><a href="function.min.html" class="function" rel="rdfs-seeAlso">min()</a> - Find lowest 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.log.html">log</a></div> <div class="next" style="text-align: right; float: right;"><a href="function.min.html">min</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>