note 120197 deleted from language.operators.comparison by cmb
| From: | cmb@php.net | Date: | Mon, 21 Nov 2016 18:04:06 +0000 |
| Subject: | note 120197 deleted from language.operators.comparison by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-207262@lists.php.net to get a copy of this message | ||
Note Submitter: MustaRoad
----
I think PHP compare arrays with <=> operator like the following:
echo [12, 11, 100] <=> [12, 20, 3];
it initiate a counter for this comparison and then compare each entry like
following:
counter for this comparison will initiated with zero:
counter = 0
after that it compare like following:
a == b this mean the count operation will be counter = counter + 0
a > b this mean the count will be counter = counter + 1
a < b this mean the count will be counter = counter - 1
12 == 12 -----> counter = counter + 0
11 < 20 -------> counter = counter - 1
100 > 3 -------> counter = counter + 1
and if the counter remain zero after these comparisons, it will try to find the first different
value and produce it as a final result, SO because the first element is 12 in both arrays the second
one will be tested and because it is different the final result will depend on it and
'echo' will produce -1 because 11 is less than 20.
test the following to be sure:
echo ([12, 100, 11] <=> [12, 3 ,20]).PHP_EOL;
echo ([12, 11, 100] <=> [12, 20 ,3]).PHP_EOL;
echo ([11, 12, 100] <=> [20 ,12 ,3]).PHP_EOL;
it just an idea I don't test it carefully BUT to help in some way thanks!!