note 77996 modified in function.import-request-variables by felipe

From: Date: Mon, 27 Oct 2008 11:48:45 +0000
Subject: note 77996 modified in function.import-request-variables by felipe
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-145797@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($v); } return $var; } ?> --was-- 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; } ?> http://php.net/manual/en/function.import-request-variables.php

« previous php.notes (#145797) next »