1.Definition
Spaceship operator
is also called combination comparison operator
orcombination comparison operator
, which is represented by the symbol <=>
, this operator can be used to compare two variables
(not limited to numeric type data).
2. Expression
$c= $a <=> $b;
If $a > $b, then the value of $c is 1;
If $a == $b, then the value of $c is 0;
If $a < $b, then the value of $c is - 1;
3. Notes
The spaceship operator is a new feature introduced in PHP7
. ##PHP7 , it is used to compare two expressions: when the first expression is less than, equal to or greater than the second expression, the value it returns is: -1, 0 or 1.
4. Usage examples
<?php //整型比较 print( 1 <=> 1);print("<br/>"); //0 print( 1 <=> 2);print("<br/>"); //-1 print( 2 <=> 1);print("<br/>"); //1 print("<br/>"); //字符型比较 print( 1.5 <=> 1.5);print("<br/>");//0 print( 1.5 <=> 2.5);print("<br/>");//-1 print( 2.5 <=> 1.5);print("<br/>");//1 print("<br/>"); //字符型 print( "a" <=> "a");print("<br/>");//0 print( "a" <=> "b");print("<br/>");//-1 print( "b" <=> "a");print("<br/>");//1 ?>
The above is the detailed content of How to use spaceship operator in PHP7. For more information, please follow other related articles on the PHP Chinese website!