note 38073 deleted from function.isset by cmb
| From: | cmb@php.net | Date: | Sun, 10 Feb 2019 11:28:02 +0000 |
| Subject: | note 38073 deleted from function.isset by cmb | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-210220@lists.php.net to get a copy of this message | ||
Note Submitter: jon
----
Since PHP will check cases in order, I often end up using this bit of code:
<?php
if (isset($var) && $var) {
// do something
}
?>
In short, if you have error reporting on, and $var is not set, PHP will generate an error if you
just have:
<?php
if ($var) { // do something }
?>
...but, as noted elsewhere, will return True if set to False in this case:
<?php
if (isset($var)) { // do something }
?>
Checking both to see if $var is set, and that it equals something other than Null or False is
something I find very useful a lot of times. If $var is not set, PHP will never execute the second
part of "(isset($var) && $var)", and thus never generate an error either.
This also works very nice for setting variable as well, e.g.:
<?php
$var = (isset($var) && $var) ? $var : 'new value';
?>