$foo (or $GLOBALS['foo'])) * Arguments are assumed to be optional, unless they are followed * by a ! (e.g. 'foo!' is 'foo' but required (still 'foo' => $foo)) * If the argument is preceeded with an = (e.g. '=foo' or '=foo!'), then * it will not be trim()'ed before the variable is set. * Also, you require that a variable be an array with [] (e.g. foo[]), * in which case args are not trimmed. * You may also pass all of the arguments as a single array. * * @param array $vars,... Optional array of vars; can be passed one-by-one * * @author davidb */ function args() { $keys = func_get_args(); if (count($keys) == 1 && is_array($keys[0])) $keys = $keys[0]; foreach ($keys as $key) { $trim = true; $arr = false; $opt = true; if (!preg_match('/^(=)?([^=\[\]!]+)(\[\])?(\!)?$/', $key, $m)) die("invalid key format in args: $key"); $key = $m[2]; ## = if ($m[1] || (isSet($m[3]) && $m[3])) $trim = false; ## [] if (isSet($m[3]) && $m[3]) $arr = true; ## ! if (isSet($m[4]) && $m[4]) $opt = false; if ($opt) $val = (array_key_exists($key, $_REQUEST)) ? $_REQUEST[$key] : ($arr ? array() : null); else { if (!array_key_exists($key, $_REQUEST)) die("Missing required argument for this page: '$key'"); $val = $_REQUEST[$key]; } if ($arr && !is_array($val)) die("Array argument {$key}[] is not an array; is: '$val'"); if (!$arr && is_array($val)) die("Scalar argument $key is an array"); if ($trim && !$arr && !is_null($val)) $val = trim($val); $GLOBALS[$key] = $val; } } ?>