svn: /phpdoc/en/trunk/reference/math/functions/ max.xml min.xml

From: Date: Mon, 01 Sep 2014 12:49:39 +0000
Subject: svn: /phpdoc/en/trunk/reference/math/functions/ max.xml min.xml
Groups: php.doc.cvs 
Request: Send a blank email to doc-cvs+get-12756@lists.php.net to get a copy of this message
daverandom Mon, 01 Sep 2014 12:49:39 +0000 Revision: http://svn.php.net/viewvc?view=revision&revision=334683 Log: Fix inconsistent and misleading description of how min() and max() treat arguments with differing types. Replace with simple explanation, and add matching examples to both pages which show various behaviours. -- Provided by anonymous 48121 (rowan.collins@gmail.com) Changed paths: U phpdoc/en/trunk/reference/math/functions/max.xml U phpdoc/en/trunk/reference/math/functions/min.xml Modified: phpdoc/en/trunk/reference/math/functions/max.xml =================================================================== --- phpdoc/en/trunk/reference/math/functions/max.xml 2014-09-01 00:08:06 UTC (rev 334682) +++ phpdoc/en/trunk/reference/math/functions/max.xml 2014-09-01 12:49:39 UTC (rev 334683) @@ -24,12 +24,11 @@ </para> <note> <para> - PHP will evaluate a non-numeric <type>string</type> as - <literal>0</literal> if compared to <type>integer</type>, but still return the string if it's seen as the - numerically highest value. If multiple arguments evaluate to - <literal>0</literal>, <function>max</function> will return a numeric - <literal>0</literal> if given, else the alphabetical highest string - value will be returned. + Values of different types will be compared using the <link linkend="language.operators.comparison"> + standard comparison rules</link>. For instance, a non-numeric <type>string</type> will be + compared to an <type>integer</type> as though it were <literal>0</literal>, but multiple + <type>string</type> values will be compared alphanumerically. The actual value returned will be of the + original type with no conversion applied. </para> </note> </refsect1> @@ -78,20 +77,10 @@ <refsect1 role="returnvalues"> &reftitle.returnvalues; <para> - <function>max</function> 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. + <function>max</function> returns the parameter value considered "highest" according to standard + comparisons. If multiple values of different types evaluate as equal (e.g. <literal>0</literal> + and <literal>'abc'</literal>) the first provided to the function will be returned. </para> - <para> - When <function>max</function> is given multiple <type>array</type>s, the - longest array is returned. If all the arrays have the same length, - <function>max</function> will use lexicographic ordering to find the return - value. - </para> - <para> - When given a <type>string</type> it will be cast as an <type>integer</type> - when comparing. - </para> </refsect1> <refsect1 role="examples"> &reftitle.examples; @@ -101,29 +90,36 @@ <programlisting role="php"> <![CDATA[ <?php -echo max(1, 3, 5, 6, 7); // 7 +echo max(2, 3, 1, 6, 7); // 7 echo max(array(2, 4, 5)); // 5 -// When 'hello' is cast as integer it will be 0. Both the parameters are equally -// long, so the order they are given in determines the result +// The string 'hello' when compared to an int is treated as 0 +// Since the two values are equal, the order they are provided determines the result echo max(0, 'hello'); // 0 echo max('hello', 0); // hello -echo max('42', 3); // '42' +// Here we are comparing -1 < 0, so 'hello' is the highest value +echo max('hello', -1); // hello -// Here 0 > -1, so 'hello' is the return value. -echo max(-1, 'hello'); // hello - // With multiple arrays of different lengths, max returns the longest $val = max(array(2, 2, 2), array(1, 1, 1, 1)); // array(1, 1, 1, 1) -// With multiple arrays of the same length, max compares from left to right -// using lexicographic order, so in our example: 2 == 2, but 4 < 5 -$val = max(array(2, 4, 8), array(2, 5, 7)); // array(2, 5, 7) +// Multiple arrays of the same length are compared from left to right +// so in our example: 2 == 2, but 5 > 4 +$val = max(array(2, 4, 8), array(2, 5, 1)); // array(2, 5, 1) -// If both an array and non-array are given, the array -// is always returned as it's seen as the largest +// If both an array and non-array are given, the array will be returned +// as comparisons treat arrays as greater than any other value $val = max('string', array(2, 5, 7), 42); // array(2, 5, 7) + +// If one argument is NULL or a boolean, it will be compared against +// other values using the rule FALSE < TRUE regardless of the other types involved +// In the below examples, -10 is treated as TRUE in the comparison +$val = max(-10, FALSE); // -10 +$val = max(-10, FALSE); // -10 + +// 0, on the other hand, is treated as FALSE, so is "lower than" TRUE +$val = max(0, TRUE); // TRUE ?> ]]> </programlisting> Modified: phpdoc/en/trunk/reference/math/functions/min.xml =================================================================== --- phpdoc/en/trunk/reference/math/functions/min.xml 2014-09-01 00:08:06 UTC (rev 334682) +++ phpdoc/en/trunk/reference/math/functions/min.xml 2014-09-01 12:49:39 UTC (rev 334683) @@ -24,12 +24,11 @@ </para> <note> <para> - PHP will evaluate a non-numeric <type>string</type> as - <literal>0</literal> if compared to <type>integer</type>, but still return the string if it's seen as the - numerically lowest value. If multiple arguments evaluate to - <literal>0</literal>, <function>min</function> will return the lowest - alphanumerical string value if any strings are given, else a numeric - <literal>0</literal> is returned. + Values of different types will be compared using the <link linkend="language.operators.comparison"> + standard comparison rules</link>. For instance, a non-numeric <type>string</type> will be + compared to an <type>integer</type> as though it were <literal>0</literal>, but multiple + <type>string</type> values will be compared alphanumerically. The actual value returned will be of the + original type with no conversion applied. </para> </note> </refsect1> @@ -78,8 +77,9 @@ <refsect1 role="returnvalues"> &reftitle.returnvalues; <para> - <function>min</function> returns the numerically lowest of the - parameter values. + <function>min</function> returns the parameter value considered "lowest" according to standard + comparisons. If multiple values of different types evaluate as equal (e.g. <literal>0</literal> + and <literal>'abc'</literal>) the first provided to the function will be returned. </para> </refsect1> <refsect1 role="examples"> @@ -93,32 +93,33 @@ echo min(2, 3, 1, 6, 7); // 1 echo min(array(2, 4, 5)); // 2 +// The string 'hello' when compared to an int is treated as 0 +// Since the two values are equal, the order they are provided determines the result echo min(0, 'hello'); // 0 echo min('hello', 0); // hello + +// Here we are comparing -1 < 0, so -1 is the lowest value echo min('hello', -1); // -1 -// With multiple arrays, min compares from left to right +// With multiple arrays of different lengths, min returns the shortest +$val = min(array(2, 2, 2), array(1, 1, 1, 1)); // array(2, 2, 2) + +// Multiple arrays of the same length are compared from left to right // so in our example: 2 == 2, but 4 < 5 $val = min(array(2, 4, 8), array(2, 5, 1)); // array(2, 4, 8) -// If both an array and non-array are given, the array -// is never returned as it's considered the largest +// If both an array and non-array are given, the array is never returned +// as comparisons treat arrays as greater than any other value $val = min('string', array(2, 5, 7), 42); // string -?> -]]> - </programlisting> - </example> - </para> - <para> - <example> - <title>Example of NULL/FALSE value with <function>min</function></title> - <programlisting role="php"> -<![CDATA[ -<?php -// PHP compares variable as boolean when one of operand is boolean. -var_dump(min(-100,-10, NULL, 10, 100)); // NULL - (bool)NULL < (bool)-100 is FALSE < TRUE -var_dump(min(-100,-10, FALSE, 10, 100)); // FALSE - (bool)FALSE < (bool)-100 is FALSE < TRUE +// If one argument is NULL or a boolean, it will be compared against +// other values using the rule FALSE < TRUE regardless of the other types involved +// In the below examples, both -10 and 10 are treated as TRUE in the comparison +$val = min(-10, FALSE, 10); // FALSE +$val = min(-10, NULL, 10); // NULL + +// 0, on the other hand, is treated as FALSE, so is "lower than" TRUE +$val = min(0, TRUE); // 0 ?> ]]> </programlisting>

« previous php.doc.cvs (#12756) next »