note 77995 added to function.import-request-variables

From: Date: Sun, 23 Sep 2007 09:46:34 +0000
Subject: note 77995 added to function.import-request-variables
Groups: php.notes 
Request: Send a blank email to php-notes+get-130706@lists.php.net to get a copy of this message
Regarding the last post: When $_GET[$k] is compared against NULL or '' (empty string) inside the foreach loop, it should be compared only against one of the expressions or strict equality operator should be used. In this case, second part of expression ($_GET[$k] == NULL) will be *never* executed, because of NULL gets converted to empty string. Also be aware that zero is equal (==) to empty string, so if passing zeroes through the $_GET, use strict comparsion to check whether variable exist or not. Next notice: when nothing will be set into $_GET array, all comparsions will generate lot of E_NOTICE errors, because you are accessing unassigned variable. <? // Slightly modified previous example $input = array('name' => null, 'age' => 26) ; // 26 is the default age, if $_GET['age'] is empty or not set /** * Extracts $_GET variables to global scope by the definition from the $input array * @return void */ function extract_get() { global $input; if (isset($input) && is_array($input)) foreach ($input as $k => $v) { if (!isset($_GET[$k])) { $GLOBALS[$k] = $v; continue; } $getval = $_GET[$k]; if ($getval === null || $getval === '') { $getval = $v; } elseif (is_numeric($v)) { $getval = (int) $getval; } elseif (get_magic_quotes_gpc() == 1) { $getval = stripslashes_deep($getval); } $GLOBALS[$k] = $getval; unset($getval); } } /** * Performs stripslashes function recursively on the array or on the single variable * @param mixed $var Variable - can be scalar variable or the array * @return mixed Variable with slashes stripped with function stripslashes() */ function stripslashes_deep($var) { if (!is_array($var)) return stripslashes($var); foreach($var as $k => $v) { $var[$k] = stripslashes_deep($var); } return $var; } ?> ---- Server IP: 212.24.129.51 Probable Submitter: 83.208.62.60 ---- Manual Page -- http://www.php.net/manual/en/function.import-request-variables.php Edit -- https://master.php.net/note/edit/77995 Del: integrated -- https://master.php.net/note/delete/77995/integrated Del: useless -- https://master.php.net/note/delete/77995/useless Del: bad code -- https://master.php.net/note/delete/77995/bad+code Del: spam -- https://master.php.net/note/delete/77995/spam Del: non-english -- https://master.php.net/note/delete/77995/non-english Del: in docs -- https://master.php.net/note/delete/77995/in+docs Del: other reasons-- https://master.php.net/note/delete/77995 Reject -- https://master.php.net/note/reject/77995 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#130706) next »