You are here: Re: iterate through function arguments by reference « PHP Programming Language « IT news, forums, messages
Re: iterate through function arguments by reference

Posted by C. on 11/17/28 11:55

amygdala wrote:
>
> I would like to iterate through arguments passed to a function, modify them
> but still let them hold there original name. To be precise: I want users to
> fill in a register form and once submitted, trim() all field values after
> being passed to the function register().
>

Yes it is possible - and its such a general question that there are
lots of different ways to do it.

The idea of modifying the data in-situ gives me a bit of a problems
though. In any user facing application there are three things you need
to do: validate, validate and VALIDATE. If you're trying to bolt on
validation to an exisitng system then it is already seriously flawed.
I, and I suspect most programmers, would want to keep the cleaned up
data well seperated from the raw data e.g. by putting it somewhere
else:

function clean($in)
{
$out=array();
foreach($in as $key=>$val) {
$out[$key]=transform($val);
}
return($out);
}

register(clean($_REQUEST));

You should also be thinking about protecting your system from injection
attacks and making the data more palatable to whatever storage
mechanism you are using...

function transform($datum)
{
$cleaned=trim($datum);
$cleaned=htmlentities($cleaned);
$cleaned=mysql_escape_string($cleaned);
return($cleaned);
}

Of course, if you really must, then $_POST, $_REQUEST et al are
writable as well as readable in your script:

$_GET=transform($_GET);

Will have the effect you want.

Note that the title of your post is a bit misleading - in the examples
above I've not modified the data pased as parameters, but it is
possible to pass references to variables instead of passing copies of
the variables - see the PHP manual for more details.

HTH

C.

 

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

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