note 127137 added to language.operators.comparison
| From: | JeffClayton at main dot php dot net | Date: | Thu, 26 May 2022 16:57:46 +0000 |
| Subject: | note 127137 added to language.operators.comparison | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-218531@lists.php.net to get a copy of this message | ||
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'
----
Server IP: 45.112.84.4
Probable Submitter: 80.90.12.131 (proxied: 172.72.88.12)
----
Manual Page -- https://php.net/manual/en/language.operators.comparison.php
Edit -- https://main.php.net/note/edit/127137
Del: integrated -- https://main.php.net/note/delete/127137/integrated
Del: useless -- https://main.php.net/note/delete/127137/useless
Del: bad code -- https://main.php.net/note/delete/127137/bad+code
Del: spam -- https://main.php.net/note/delete/127137/spam
Del: non-english -- https://main.php.net/note/delete/127137/non-english
Del: in docs -- https://main.php.net/note/delete/127137/in+docs
Del: other reasons-- https://main.php.net/note/delete/127137
Reject -- https://main.php.net/note/reject/127137
Search -- https://main.php.net/manage/user-notes.php