Click here to Skip to main content
15,846,038 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
How does the PHP spaceship operator work on the strings ?
PHP
var_dump("A" <=> "Z"); // int(-1)

The result is “-1”, Could you tell me, why?
The “A” char is the first letter and “Z” is the last letter in the alphabet!

So, what is the reason to give us this result ?

I know how does the spaceship work with other PHP data type such as integer, boolean etc. But in the PHP string, I am not sure.

What I have tried:

I used the var_dump function to print the value and type but it gave me "-1", So, could you tell me why?
Posted
Updated 1-Sep-22 9:37am
v3

 
Share this answer
 
The spaceship operator does a comparison where:

  • < : Less than
  • = : Equal to
  • > : Greater than

The value returned is a numeric value indicating the result of the comparison. For string values I believe it does something akin to the PHP: strcmp - Manual[^] function, except the values are capped at -1, 0 and 1. The documentation itself stipulates that it should only ever return one of these 3 values. If you wanted the actual, fully compared value then use strcmp instead.
 
Share this answer
 
Comments
Michael Para 2-Sep-22 3:06am    
This answer doesn't complete, the PHP strings are working with the ASCII system and that is happening when you use the spaceship operator to compare two string values.
Chris Copeland 2-Sep-22 4:49am    
This comment doesn't make sense, and I assume you also down-voted my answer? ASCII has nothing to do with the spaceship operator, my answer explicitly states that the operator functions much like the strcmp function but capped to -1, 0 and 1.
Actually, this is a new feature for PHP7. The string is working there according to the ASCII of letters. This means the American Standard Code for Information Interchange.

So the “A“ letter has this value (65) according to the ASCII
And the “Z“ letter has also this value (90) according to the ASCII

So the A(65) is less than the Z(90) which would show you this result int(-1)
 
Share this answer
 
v3
Comments
Patricia Hernandez 2022 1-Sep-22 10:13am    
Thank you so much
Quote:
It shows me -1, why? A is the first letter and Z is the last letter!
Because it is designed this very way.
Since "A" comes first than "Z", then "A" is less than "Z" (the same behavior you have with numbers, e.g. 1 comes first than 5). See PHP: New features - Manual[^]
 
Share this answer
 
This is a new feature of PHP 7 you can read more about it in PHP Manual.
 
Share this answer
 
v4
Comments
OriginalGriff 26-Nov-22 2:00am    
This adds nothing to the discussion, and thus looks like what we call "rep point hunting" - adding dubious solutions in order to artificially inflate your reputation with the site. We consider that a form of site abuse, and ban users when we find them persisting in it.

That may not have been your intention, but I would strongly advise that you answer fresh questions only, and with a solution that adds to the conversation rather than generic waffle. If you don't, you will be kicked from the site.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900