Home > Backend Development > PHP7 > body text

How to use spaceship operator in PHP7

autoload
Release: 2023-02-17 20:44:02
Original
2088 people have browsed it

1.Definition

Spaceship operator is also called combination comparison operatororcombination 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;
Copy after login
  • 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
?>
Copy after login
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!

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template