6

In PHP 7 we have a new operator, spaceship operator <=>, and I found it very similar (if not the same) to strcmp().

Is there any difference between them?

Edit: Im asking the difference between them both, not refered What is <=> (the 'Spaceship' Operator) in PHP 7? or What is <=> (the 'Spaceship' Operator) in PHP 7?

Community
  • 1
  • 1
Shapi
  • 5,493
  • 4
  • 28
  • 39
  • 1
    Possible duplicate of [What is <=> (the 'Spaceship' Operator) in PHP 7?](http://stackoverflow.com/questions/30365346/what-is-the-spaceship-operator-in-php-7) – Marcos Pérez Gude May 11 '16 at 16:17
  • From the [RFC](https://wiki.php.net/rfc/combined-comparison-operator) `Similar to strcmp() or version_compare() in behavior, but it can be used on all generic PHP values with the same semantics as <, <=, ==, >=, >.`. – Jonnix May 11 '16 at 16:18
  • @MarcosPérezGude ive saw that one and does not answer my question. my question is what is the difference, and I do not see any ref to it. – Shapi May 11 '16 at 16:20
  • 1
    `strcmp` compares strings, `<=>` compares different datatypes. – u_mulder May 11 '16 at 16:24
  • Ok, sorry, i retract my vote – Marcos Pérez Gude May 11 '16 at 16:36

3 Answers3

5

strcmp - it is function for "binary safe" string comparison

The spaceship operator (<=>) returns -1 if the left side is smaller, 0 if the values are equal and 1 if the left side is larger. It can be used on all generic PHP values with the same semantics as < , <=, ==, >=, >. This operator is similar in behavior to strcmp() or version_compare(). This operator can be used with integers, floats, strings, arrays, objects, etc.

For example you can compare arrays or objects, and by float you get different result:

$var1 = 1.3;
$var2 = 3.2;
var_dump($var1 <=> $var2); // int(-1)
var_dump(strcmp($var1, $var2)); // int(-2)

And other differences...

More example this

Maxim Tkach
  • 1,607
  • 12
  • 23
0

According to the official document:

"<=>" returns "an integer less than, equal to, or greater than zero" while "strcmp" retuens "<0 , =0 or >0", so you may not find any difference there.

And usually, this is enough because we don't care what exact value is returned, but, something is revealed as below:

echo 5 <=> 1; // 1
echo strcmp(5,1); // 4

I could never get values other than 1,0,-1 from spaceship.

John Lee
  • 39
  • 2
  • There's probably a missing documentation, because "new features" document described `-1, 0, 1` explicitly: https://www.php.net/manual/en/migration70.new-features.php however operators docs say just `an integer` https://www.php.net/manual/en/language.operators.comparison.php – jave.web Jun 16 '22 at 12:32
0
  • <=> always returns -1, 0, or 1. strcmp() on the other hand usually returns integers smaller than -1 or larger then 1: the character code difference of the first differing characters.
  • <=> can be used on all generic PHP values, while strcmp() compares strings only.
  • When both are applied on strings, <=> and strcmp() yield the same sign, though not necessarily the same absolute values.
  • When used on different types, the rules of Comparison with Various Types apply for <=>, whereas arguments to strcmp() are first cast to string. This may lead to significantly different results as shown in the following examples:
// Int 19 is converted to string '19', int 120 is converted to string '120',
// then only the first differing chars (the second ones) count.
echo strcmp(19, 120), "\n"; // == 7, as '9' is larger than '2'
// No conversion, numeric comparison.
echo 19 <=> 100, "\n\n";    // == -1

// Bool false is converted to string '', int 0 is converted to string '0'.
echo strcmp(false, 0), "\n"; // == -1
// Int 0 is converted to false.
echo false <=> 0, "\n\n";    // == 0

// Bool true is converted to string '1', which is "smaller" than letter 't'.
echo strcmp(true, 'true'), "\n"; // == ord('1') - ord('t') == -67
// String 'true' is converted to bool true, since it is not `empty()`.
echo true <=> 'true', "\n\n";    // == 0

// Int 1 is converted to string '1', which is greater then space (' ').
echo strcmp(1, ' 2'), "\n"; // == 17
// String ' 2' is converted to int 2.
echo 1 <=> ' 2', "\n\n";    // == -1
Steve4585
  • 36
  • 4