Skip to content

Instantly share code, notes, and snippets.

@itsliamjones
Last active May 10, 2016 09:24
Show Gist options
  • Save itsliamjones/fcf3292e9b31d1c9ff84b9f74f750a8b to your computer and use it in GitHub Desktop.
Save itsliamjones/fcf3292e9b31d1c9ff84b9f74f750a8b to your computer and use it in GitHub Desktop.
PHP5 Three way comparison (Spaceship Operator)
<?php
/**
* PHP5 Three way comparison
*
* PHP7 This wouldn't be needed, instead you would do something along the lines of -
* if/switch ($left <=> $right) { ... }
*
* @param integer $left
* @param integer $right
*
* @return integer
*/
function threeWayComparison($left, $right)
{
if ($left < $right) {
return -1;
} elseif ($left > $right) {
return 1;
} else {
return 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment