Home >Backend Development >PHP7 >How to use spaceship operator in PHP7

How to use spaceship operator in PHP7

autoload
autoloadOriginal
2021-03-16 16:58:122146browse

1.Definition

Spaceship operator is also called combination comparison operatororcombination comparison operator, which is represented by the symbol 96b4fef55684b9312718d5de63fb7121, 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
?>

Recommended:

php video tutorial

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Related articles

See more