note 78485 added to function.isset
| From: | petruzanauticoyahoo?com!ar at osu1 dot php dot net | Date: | Sun, 14 Oct 2007 18:22:36 +0000 |
| Subject: | note 78485 added to function.isset | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-131383@lists.php.net to get a copy of this message | ||
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.
----
Server IP: 201.216.232.3
Probable Submitter: 24.232.22.244
----
Manual Page -- http://www.php.net/manual/en/function.isset.php
Edit -- https://master.php.net/note/edit/78485
Del: integrated -- https://master.php.net/note/delete/78485/integrated
Del: useless -- https://master.php.net/note/delete/78485/useless
Del: bad code -- https://master.php.net/note/delete/78485/bad+code
Del: spam -- https://master.php.net/note/delete/78485/spam
Del: non-english -- https://master.php.net/note/delete/78485/non-english
Del: in docs -- https://master.php.net/note/delete/78485/in+docs
Del: other reasons-- https://master.php.net/note/delete/78485
Reject -- https://master.php.net/note/reject/78485
Search -- https://master.php.net/manage/user-notes.php