note 123554 added to function.strcmp
| From: | mikael1.mail.ru via php-notes | Date: | Sun, 27 Jan 2019 15:05:37 +0000 |
| Subject: | note 123554 added to function.strcmp | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-210121@lists.php.net to get a copy of this message | ||
1) If the two strings have identical BEGINNING parts, they are trunkated from both strings.
2) The resulting strings are compared with two possible outcomes:
a) if one of the resulting strings is an empty string, then the length of the non-empty string is
returned (the sign depending on the order in which you pass the arguments to the function)
b) in any other case just the numerical values of the FIRST characters are compared. The result is
+1 or -1 no matter how big is the difference between the numerical values.
<?php
$str = array('','a','afox','foxa');
$size = count($str);
echo '<pre>';
for($i=0; $i<$size; $i++)
{
for($j=$i+1; $j<$size; $j++)
{
echo '<br>('.$str[$i].','.$str[$j].') = '.strcmp($str[$i],
$str[$j]);
echo '<br>('.$str[$j].','.$str[$i] .') = '.strcmp($str[$j],
$str[$i]);
}
}
echo '</pre>';
?>
In Apache/2.4.37 (Win32) OpenSSL/1.1.1 PHP/7.2.12 produces the following results:
(,a) = -1 //comparing with an empty string produces the length of the NON-empty string
(a,) = 1 // ditto
(,afox) = -4 // ditto
(afox,) = 4 // ditto
(,foxa) = -4 // ditto
(foxa,) = 4 // ditto
(a,afox) = -3 // The identical BEGINNING part ("a") is trunkated from both strings. Then
the remaining "fox" is compared to the remaing empty string in the other argument.
Produces the length of the NON-empty string. Same as in all the above examples.
(afox,a) = 3 // ditto
(a,foxa) = -1 // Nothing to trunkate. Just the numerical values of the first letters are compared
(foxa,a) = 1 // ditto
(afox,foxa) = -1 // ditto
(foxa,afox) = 1 // ditto
----
Server IP: 208.43.231.9
Probable Submitter: 185.85.3.142 (proxied: 37.203.23.119)
----
Manual Page -- http://php.net/manual/en/function.strcmp.php
Edit -- https://master.php.net/note/edit/123554
Del: integrated -- https://master.php.net/note/delete/123554/integrated
Del: useless -- https://master.php.net/note/delete/123554/useless
Del: bad code -- https://master.php.net/note/delete/123554/bad+code
Del: spam -- https://master.php.net/note/delete/123554/spam
Del: non-english -- https://master.php.net/note/delete/123554/non-english
Del: in docs -- https://master.php.net/note/delete/123554/in+docs
Del: other reasons-- https://master.php.net/note/delete/123554
Reject -- https://master.php.net/note/reject/123554
Search -- https://master.php.net/manage/user-notes.php