PHP स्पेसशिप ऑपरेटर <=> स्ट्रिंग्स पर कैसे काम करता है?
मैं स्ट्रिंग पर स्पेसशिप ऑपरेटर के कार्य के बारे में उलझन में हूँ। में प्रलेखन वे कहते हैं कि तुलना PHP के सामान्य प्रकार के अनुसार प्रदर्शन कर रहे हैं तुलना नियमों , लेकिन अभी तक मेरे लिए स्पष्ट नहीं! मैंने इस स्टैकओवरफ्लो प्रश्न को देखा और कुछ परीक्षण किए लेकिन फिर भी उलझन में है।
यहाँ कोड मैंने परीक्षण किया है:
<?php
$str1 = "aaa"; $str2 = "aaaa";
echo $str1 <=> $str2, PHP_EOL; // -1
$str1 = "baaaaaa"; $str2 = "abbb";
echo $str1 <=> $str2, PHP_EOL; // 1
$str1 = "aaaaaaa"; $str2 = "bbbb";
echo $str1 <=> $str2, PHP_EOL; // -1
यह ASCII मूल्यों का उपयोग कैसे करता है? मदद के लिए शुक्रिया।
जवाब
दो भावों की तुलना करें।
स्ट्रिंग के लिए यह ASCII मूल्यों का उपयोग करता है।
It returns -1, 0 or 1 when first expression is respectively less than, equal to, or greater than second expression.
// Integers
echo 1 <=> 1; // 0
echo 1 <=> 2; // -1
echo 2 <=> 1; // 1
// Floats
echo 1.5 <=> 1.5; // 0
echo 1.5 <=> 2.5; // -1
echo 2.5 <=> 1.5; // 1
// Strings
echo "a" <=> "a"; // 0
echo "a" <=> "b"; // -1
echo "b" <=> "a"; // 1
echo "a" <=> "aa"; // -1
echo "zz" <=> "aa"; // 1
// Arrays
echo [] <=> []; // 0
echo [1, 2, 3] <=> [1, 2, 3]; // 0
echo [1, 2, 3] <=> []; // 1
echo [1, 2, 3] <=> [1, 2, 1]; // 1
echo [1, 2, 3] <=> [1, 2, 4]; // -1
// Objects
$a = (object) ["a" => "b"]; $b = (object) ["a" => "b"];
echo $a <=> $b; // 0
$a = (object) ["a" => "b"]; $b = (object) ["a" => "c"];
echo $a <=> $b; // -1
$a = (object) ["a" => "c"]; $b = (object) ["a" => "b"];
echo $a <=> $b; // 1
// not only values are compared; keys must match
$a = (object) ["a" => "b"]; $b = (object) ["b" => "b"];
echo $a <=> $b; // 1
When you use on strings, it will go from left to right, and compare each character in the given string to finds a first difference, then it will comparing the ASCII value of this just found different character. So:
"aaa" < "aaaa" // because 4th char: '' < 'a'
"baaaaaa" > "abbb" // because 1st char 'b' > 'a'
"aaaaaaa" < "bbbb" // because 1st char 'a' < 'b'
And, it is a simple string spaceship operator
$str1 <=> $str2 will return :
1 if $str1 > $str2
-1 if $str1 < $str2
0 if $str1 = $str2
The String comparison is based on the ASCII code of each letter and the alphabetical order.
All the strings starting with lower case letters will be greater than strings starting with uppercase letters (because in ASCII, uppercase letters are represented by numbers from 65 to 90, while lowercase letters code are from 97 to 122)
The comparison is done letter by letter and is stopped as soon as two letters of the same position are different.
This, combined with the alphabetical order will give for example:
// "a" comes before "aa" so:
"a" < "aa"
// and
"a" <=> "aa" === -1
// "b" comes after "B" so:
"b" > "B"
// and
"b" <=> "B" === 1
"Aa" === "Aa"
// and
"Aa" <=> "Aa" === 0
// Because uppercase < lowercase
// "Abb" comes before "abb"
"Abb" < "abb"
"Abb" <=> "abb" === -1
// Because uppercase < lowercase
"Abb" < "a"
"Abb" <=> "a" === -1
// Because uppercase < lowercase
"Bbb" < "a"
"Bbb" <=> "a" === -1
//
"Cbb" > "Baa"
"Cbb" <=> "Baa" === 1