|
Posted by Oli Filth on 11/18/01 11:44
Jerry Stuckle said the following on 08/04/2006 16:11:
> Tim Martin wrote:
>>
>> function get_post_var_with_default($name, $default = 'default value')
>> {
>> if (isset($_POST[$name]))
>> {
>> return $_POST[$name];
>> }
>> else
>> {
>> return $default;
>> }
>> }
>>
>> then later
>>
>> $var = get_post_var_with_default('postvar');
>>
>> Am I missing something here?
>>
> And you'd need another one for $_SESSION, and another one for $_GET.....
>
> And what if you want to do it for a non-super global - i.e. an array
> member?
>
> With macros you need only one, i.e.
>
> $var = GET_WITH_DEFAULT($_POST['myvar']), 'default value');
> $var2 = GET_WITH_DEFAULT_VALUE($myarray['test'], 'default_value);
>
function getWithDefault(&$array, $key, $default = NULL)
{
return (isset($array[$key])) ? $array[$key] : $default;
}
--
Oli
Navigation:
[Reply to this message]
|