note 125830 added to language.operators.logical
| From: | egst@php.net | Date: | Tue, 16 Feb 2021 11:23:33 +0000 |
| Subject: | note 125830 added to language.operators.logical | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-219251@lists.php.net to get a copy of this message | ||
In response to Lawrence about || always returning a boolean:
Instead of
$x ? $x : 'fallback'
you can also use the "elvis operator":
$x ?: 'fallback'
which is useful in cases, where the left-hand side of the ternary operator is too long type, is too
complex to calculate twice, or has side-effects.
It also combines nicely with the ?? operator, which is equivalent to an empty() check (both isset()
and
!= false):
$x->y ?? null ?: 'fallback';
instead of:
empty($x->y) ? $x->y : 'fallback'
----
Server IP: 45.112.84.4
Probable Submitter: 45.112.85.245 (proxied: 185.14.232.154)
----
Manual Page -- http://php.net/manual/en/language.operators.logical.php
Edit -- https://master.php.net/note/edit/125830
Del: integrated -- https://master.php.net/note/delete/125830/integrated
Del: useless -- https://master.php.net/note/delete/125830/useless
Del: bad code -- https://master.php.net/note/delete/125830/bad+code
Del: spam -- https://master.php.net/note/delete/125830/spam
Del: non-english -- https://master.php.net/note/delete/125830/non-english
Del: in docs -- https://master.php.net/note/delete/125830/in+docs
Del: other reasons-- https://master.php.net/note/delete/125830
Reject -- https://master.php.net/note/reject/125830
Search -- https://master.php.net/manage/user-notes.php