Reputation: 35
Why this only print out "-1". What happens to the first part of the text.
echo "2 results of this " . "Apples" <=> "bananas";
Thanks
Upvotes: 0
Views: 126
Reputation: 15464
Because it will 1st concatenate the string and then will apply the spaceship operator comparison
"
2 results of this " . "Apples" <=> "bananas"; = -1
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ^^^^^^^^^
string1 string2
try below
$spaceship_result= "Apples" <=> "bananas";
echo "2 results of this ".$spaceship_result;
Upvotes: 1
Reputation: 781731
.
has higher precedence than <=>
, so it's parsed as if you'd written:
echo ("2 results of this " . "Apples") <=> "bananas";
which is equivalent to:
echo "2 results of this Apples" <=> "bananas";
So it's comparing these two strings and printing just the result.
Add parentheses to get what you want:
echo "2 results of this " . ("Apples" <=> "bananas");
Upvotes: 3