You are here: Re: Macros in php « PHP Programming Language « IT news, forums, messages
Re: Macros in php

Posted by Jerry Stuckle on 11/14/09 11:44

Tim Martin wrote:
> Emil wrote:
>
>> Kimmo Laine napisał(a):
>>
>>>> $var = isset($_POST['postvar']) ? $_POST['postvar'] . 'default
>>>> value';
>>>>
>>
>> I have the same problem.
>>
>>
>>>
>>> And tell mme again why you couldn't write a function instead of a
>>> macro to do that?
>>
>>
>> The reason is when you pass $_POST['postvar'] to a function and
>> $_POST['postvar'] is not set, PHP generates warning. Of course one
>> could turn off warnings and usually does, but in my opinion it's not a
>> solution.
>
>
> Surely $_POST is a superglobal and will thus be available within the
> function scope just as it would be within the macro "scope".
>
> Couldn't you do something like this (untested code, function name is
> deliberately bad)?
>
> 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?
>
> Tim
> *** Free account sponsored by SecureIX.com ***
> *** Encrypt your Internet usage with a free VPN account from
> http://www.SecureIX.com ***

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);


--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

 

Navigation:

[Reply to this message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация