Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:70178 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 80589 invoked from network); 17 Nov 2013 18:37:11 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 17 Nov 2013 18:37:11 -0000 Authentication-Results: pb1.pair.com smtp.mail=rowan.collins@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rowan.collins@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 74.125.82.169 as permitted sender) X-PHP-List-Original-Sender: rowan.collins@gmail.com X-Host-Fingerprint: 74.125.82.169 mail-we0-f169.google.com Received: from [74.125.82.169] ([74.125.82.169:49333] helo=mail-we0-f169.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 6E/00-14064-7DC09825 for ; Sun, 17 Nov 2013 13:37:11 -0500 Received: by mail-we0-f169.google.com with SMTP id t60so390602wes.28 for ; Sun, 17 Nov 2013 10:37:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=message-id:date:from:user-agent:mime-version:to:subject:references :in-reply-to:content-type; bh=XxjaHZAcSaJF57fVMk+b51/GXMcfU6vmQQOLwohZZKU=; b=DiSJqHd7rzhpED7DVH1bSa5eopxJl8FQFqnkEOetviyHGgdY9E30xwZS60xf/qgI6U umTvbjgd7Ktb3LZWzhKBjXJ5h+YciszX5W3lBlxDxKNas3ct5XV+HZ4Hn0rcPyeaAq0i 9Pti0YZ5CFW6upCMwVHksK1DjnVppxOrfLv8aVJmDU5czFuH7cf+5bmlLg3hGlbrfhYB gKdsreYnU/eXPwiNG7r7HJPHKv1L6/lHBODDwaRkT3cTsJT5U3apO9IXdCi7LC1DbfZu dK227uRWUF/5Xj6fOrEoUaMygXK3xey+MsoWqXJoVlZoaJzmppmu6KKszeS+VTgQknmE 4O6A== X-Received: by 10.180.79.163 with SMTP id k3mr14239535wix.34.1384713427969; Sun, 17 Nov 2013 10:37:07 -0800 (PST) Received: from [192.168.0.2] (cpc19-brig17-2-0-cust25.3-3.cable.virginm.net. [81.101.201.26]) by mx.google.com with ESMTPSA id ey4sm16424429wic.11.2013.11.17.10.37.06 for (version=TLSv1 cipher=ECDHE-RSA-RC4-SHA bits=128/128); Sun, 17 Nov 2013 10:37:06 -0800 (PST) Message-ID: <52890CC5.8060902@gmail.com> Date: Sun, 17 Nov 2013 18:36:53 +0000 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:24.0) Gecko/20100101 Thunderbird/24.1.0 MIME-Version: 1.0 To: internals@lists.php.net References: <527DF228.1080108@sugarcrm.com> <416659913.20131110035553@cypressintegrated.com> <127298508.20131110042057@cypressintegrated.com> <97776909.20131110045342@cypressintegrated.com> <527FDE61.70102@gmail.com> <346085145.20131110145938@cypressintegrated.com> <52812873.70406@gmail.com> <1755993748.20131111140340@cypressintegrated.com> In-Reply-To: <1755993748.20131111140340@cypressintegrated.com> Content-Type: multipart/mixed; boundary="------------010703090802070304080607" Subject: Re: [PHP-DEV] [RFC] Comparison and conversion inconsistency - need more info From: rowan.collins@gmail.com (Rowan Collins) --------------010703090802070304080607 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit On 11/11/2013 19:03, Sanford Whiteman wrote: >> You don't have to think the current documentation is awful to find ways >> of improving it. > Sure, go for it! > > -- S. I've had a busy week, but have now submitted a patch via edit.php.net (copy attached) which tidies up both min() and max() documentation to refer to the standard comparisons rather than the misleading mentions of "numeric comparison" etc. I've also made them use the same examples wherever it makes sense, since they really are exact counter-parts to one another. Any feedback is of course welcome. :) -- Rowan Collins [IMSoP] --------------010703090802070304080607 Content-Type: text/plain; charset=windows-1252; name="patch-1384689936.patch" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="patch-1384689936.patch" Index: en/reference/math/functions/min.xml --- en/reference/math/functions/min.xml +++ en/reference/math/functions/min.xml @@ -24,12 +24,11 @@ - PHP will evaluate a non-numeric string as - 0 if compared to integer, but still return the string if it's seen as the - numerically lowest value. If multiple arguments evaluate to - 0, min will return the lowest - alphanumerical string value if any strings are given, else a numeric - 0 is returned. + Values of different types will be compared using the + standard comparison rules. For instance, a non-numeric string will be + compared to an integer as though it were 0, but multiple + string values will be compared alphanumerically. The actual value returned will be of the + original type with no conversion applied. @@ -78,8 +77,9 @@ &reftitle.returnvalues; - min returns the numerically lowest of the - parameter values. + min returns the parameter value considered "lowest" according to standard + comparisons. If multiple values of different types evaluate as equal (e.g. 0 + and 'abc') the first provided to the function will be returned. @@ -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 -?> -]]> - - - - - - Example of NULL/FALSE value with <function>min</function> - - ]]> Index: en/reference/math/functions/max.xml --- en/reference/math/functions/max.xml +++ en/reference/math/functions/max.xml @@ -24,12 +24,11 @@ - PHP will evaluate a non-numeric string as - 0 if compared to integer, but still return the string if it's seen as the - numerically highest value. If multiple arguments evaluate to - 0, max will return a numeric - 0 if given, else the alphabetical highest string - value will be returned. + Values of different types will be compared using the + standard comparison rules. For instance, a non-numeric string will be + compared to an integer as though it were 0, but multiple + string values will be compared alphanumerically. The actual value returned will be of the + original type with no conversion applied. @@ -78,19 +77,9 @@ &reftitle.returnvalues; - max 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. - - - When max is given multiple arrays, the - longest array is returned. If all the arrays have the same length, - max will use lexicographic ordering to find the return - value. - - - When given a string it will be cast as an integer - when comparing. + max returns the parameter value considered "highest" according to standard + comparisons. If multiple values of different types evaluate as equal (e.g. 0 + and 'abc') the first provided to the function will be returned. @@ -101,29 +90,36 @@ -1, so 'hello' is the return value. -echo max(-1, 'hello'); // hello +// Here we are comparing -1 < 0, so 'hello' is the highest value +echo max('hello', -1); // 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 ?> ]]> --------------010703090802070304080607--