1.

Php 7 - Spaceship Operator?

Answer»

In PHP 7, a new feature, spaceship operator has been introduced. It is used to compare two expressions. It returns -1, 0 or 1 when FIRST expression is respectively less than, equal to, or greater than second expression.

Example

");

print( 1 <=> 2);print("
");

print( 2 <=> 1);print("
");

print("
");

//float comparison

print( 1.5 <=> 1.5);print("
");

print( 1.5 <=> 2.5);print("
");

print( 2.5 <=> 1.5);print("
");

print("
");

//string comparison

print( "a" <=> "a");print("
");

print( "a" <=> "b");print("
");

print( "b" <=> "a");print("
");

?>

It produces the FOLLOWING BROWSER OUTPUT

0

-1

1

 0

-1

1

 0

-1

1

In PHP 7, a new feature, spaceship operator has been introduced. It is used to compare two expressions. It returns -1, 0 or 1 when first expression is respectively less than, equal to, or greater than second expression.

Example

");

print( 1 <=> 2);print("
");

print( 2 <=> 1);print("
");

print("
");

//float comparison

print( 1.5 <=> 1.5);print("
");

print( 1.5 <=> 2.5);print("
");

print( 2.5 <=> 1.5);print("
");

print("
");

//string comparison

print( "a" <=> "a");print("
");

print( "a" <=> "b");print("
");

print( "b" <=> "a");print("
");

?>

It produces the following browser output −

0

-1

1

 0

-1

1

 0

-1

1



Discussion

No Comment Found