note 117218 modified in language.operators.logical by rasmus
| From: | rasmus@php.net | Date: | Tue, 05 May 2015 04:59:55 +0000 |
| Subject: | note 117218 modified in language.operators.logical by rasmus | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-202429@lists.php.net to get a copy of this message | ||
Assign a value to a variable if it isn't defined
<?php
isset($x) || $x = 123;
print $x; // 123
?>
instead of:
<?php
if ( !isset($x)) {
$x = 123;
}
// or
$x = isset($x) ? $x : 123;
// or
$x = isset($x) ?: 123;
editor's note: In PHP 7 you could use the coalesce operator and do:
$x = $x ?? 123;
--was--
Assign a value to a variable if it isn't defined
<?php
isset($x) || $x = 123;
print $x; // 123
?>
instead of:
<?php
if ( !isset($x)) {
$x = 123;
}
// or
$x = isset($x) ? $x : 123;
// or
$x = isset($x) ?: 123;
http://php.net/manual/en/language.operators.logical.php