Spaceship operator

Posted in: Articles by musiitwa joseph 2016-03-04 at 19:15:21

image for Spaceship operator

The spaceship operator or combined compasion operator is a combination of three operators, that is less than (<), equal (=) and greater than (>). The operator is used in between two operands which are numbers.

<?php
$number = 2016 <=> 2015
?>

The operator checks if the left operand is less than the right operand, if true it returns -1 (2016 < 2015 ? return -1)

Else the operator checks if the left operand is equal to the right operand, if true it returns 0 (2016 = 2015 ? return 0)

Else the operator checks if the left operand is greater than the right operand, if true it returns 1 (2016 < 2015 ? return 1)

The good thing about this operator is that one of the conditions is always true.

Tags:
php

Search

Leave a comment

Fields with * are required
Your email address will not be displayed
3 + 3 =