note 127137 deleted from language.operators.comparison by crell
| From: | crell@php.net | Date: | Thu, 26 May 2022 23:01:39 +0000 |
| Subject: | note 127137 deleted from language.operators.comparison by crell | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-218532@lists.php.net to get a copy of this message | ||
Note Submitter: Jeff Clayton
----
Simple usage of spaceship comparison operator with a switch statement:
function spaceship_compare($a=NULL, $b=NULL)
{
$ret = NULL;
if (isset($a) && isset($b))
{
switch ($a <=> $b)
{
case -1:
$ret = strval($a).' < '.strval($b);
break;
case 0:
$ret = strval($a).' = '.strval($b);
break;
case 1:
$ret = strval($a).' > '.strval($b);
break;
default:
$ret = "Unspecified Error"; // this shouldn't ever happen
break;
}
}
return $ret;
}
$a=5; $b=18; $c=spaceship_compare($a,$b); echo ($c!=NULL?$c:'NULL')."<br
/>"; // '5 < 18'
$a=5; $b=$a; $c=spaceship_compare($a,$b); echo ($c!=NULL?$c:'NULL')."<br
/>"; // '5=5'
$a=19; $b=8; $c=spaceship_compare($a,$b); echo ($c!=NULL?$c:'NULL')."<br
/>"; // '19 > 8'
$c=spaceship_compare(); echo ($c!=NULL?$c:'NULL')."<br />"; //
'NULL'