Home  >  Article  >  Backend Development  >  How to use the combined comparison operator (<=>) in PHP7? (code example)

How to use the combined comparison operator (<=>) in PHP7? (code example)

青灯夜游
青灯夜游Original
2019-02-15 11:57:264353browse

The combined comparison operator () is a very useful operator. This article will show you how to use the combined comparison operator (). I hope it will be useful to you. help.

How to use the combined comparison operator (<=>) in PHP7? (code example)

Combined comparison operator ()

## operation operator is a three-way comparison operator that performs greater than, less than, and equality comparisons between two operands. [Video tutorial recommendation:

PHP tutorial]

Example:

$c = $a <=> $b;
// 这相当于
$c = ($a < $b) ? -1 : (($a > $b) ? 1 : 0);

Explanation: The operator behaves like strcmp( ) or version_compare(); it can be used with integers, floats, strings, arrays, objects, etc.

The combined comparison provided by the operator:

● If the values ​​on both sides are equal, 0 is returned

● If the left If the value on the right side is greater, return 1

● If the value on the right side is greater, return -1

Code example

Let’s use code examples to see how the operator performs combined comparisons.

Example 1: Integer comparison

<?php 
echo"整数 <br>"; 
echo 7 <=> 7 ; 
echo"<br>"; 
echo 7 <=> 6; 
echo"<br>"; 
echo 6 <=> 7; 
?>

Rendering:

How to use the combined comparison operator (<=>) in PHP7? (code example)

Example 2: Floating point number Comparison

<?php 
echo"浮点数<br>"; 
  
echo 2.5 <=> 1.5;  
echo"<br>"; 
echo 0.5 <=> 1.5;  
echo"<br>"; 
echo 1.5 <=> 1.5;  
?>

Rendering:

How to use the combined comparison operator (<=>) in PHP7? (code example)

Example 3: String comparison

<?php 
echo"<br>字符串<br>"; 
echo "a" <=> "a" ; 
echo"<br>"; 
echo "g" <=> "b" ;  
echo"<br>"; 
echo "a" <=> "b" ;  
echo"<br>"; 
echo "A" <=> "B" ;  
echo"<br>"; 
echo "a" <=> "B" ;  
echo"<br>"; 
echo "2" <=> "1" ;  
echo"<br>"; 
echo "2" <=> "a" ;  
echo"<br>"; 
echo "2" <=> "A" ;  
?>

Rendering :

How to use the combined comparison operator (<=>) in PHP7? (code example)

Description: String comparison size, the comparison is the value of ascii code. The following are the ascii codes corresponding to some characters

● “0”~”9”: 48~57

● “A”~“Z”: 65~90

● “a”~”z”: 97~122

Example 4: Array comparison

<?php 
echo"<br>数组<br>"; 
echo [] <=> [];  
echo"<br>"; 
echo [1, 7, 3] <=> [1, 7, 3]; 
echo"<br>"; 
echo [1, 7, 3, 5] <=> [1, 7, 3]; 
echo"<br>"; 
echo [1, 7, 3] <=> [4, 4, 4]; 
echo"<br>"; 
?>

Rendering:


How to use the combined comparison operator (<=>) in PHP7? (code example)

The above is the entire content of this article, I hope it will be helpful to everyone's study. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of How to use the combined comparison operator (<=>) in PHP7? (code example). 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