note 78485 deleted from function.isset by sobak
| From: | sobak@php.net | Date: | Sat, 10 Dec 2016 09:54:31 +0000 |
| Subject: | note 78485 deleted from function.isset by sobak | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-208417@lists.php.net to get a copy of this message | ||
Note Submitter: petruzanauticoyahoo?com!ar
----
I've come up with a little not-so-clean but useful function to avoid checking if a variable is
set before reading its value, specially useful for $_REQUEST and the like:
<?php
function toLocal( $source, &$dest, $default = null )
{
if( isset( $source ) )
$dest = $source;
else
$dest = $default;
}
?>
and then call it this way:
<?php
@toLocal( $_REQUEST['item_id'], $item_id, 0 );
?>
It checks wether the variable is set, copies it to a local variable, and if it wasn't set, it
assigns the new variable a default value, all in one line, preventing you to have to always check
for isset() before trying to read its value.
Gotta call it with @ because if the variable is not set, then trying to pass it as an argument will
yield a warning.
Petruza.