473,385 Members | 1,478 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

iterate through function arguments by reference

Hi again,

Thanks for taking the time again to help me out. Much appreciated. I'm
pretty new to PHP so I'm exploring the possibilities of the language a bit.

Here's my problem:

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().

Maybe my effort so far will clear it up even more:

// original list of arguments is quite longer
function register( $firstname, $lastname, $birthdate )
{
// loop through all arguments and modify them by reference
// someting like:
foreach( func_get_args() as $key =$value )
{
$key = trim( $val );
}
// echo trimmed argument $firstname
echo $firstname;
}

Is that possible?

Cheers

Aug 14 '06 #1
3 2656
C.
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.

Aug 14 '06 #2
C.
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.

Aug 14 '06 #3

"C." <co************@gmail.comschreef in bericht
news:11*********************@i42g2000cwa.googlegro ups.com...
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.
I see your points, and I am aware of the importance of validating user input
and preventing SQL injection. I'm using PDO's prepare function and execute
statement, amongst other things.

I didn't see much danger in overwriting user input arguments by cleaned up
values though. Anyway, your suggestions were not what I had in mind exactly
but it looks like your approach is the smarter thing to do anyway.

Thanks!
Aug 15 '06 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: EsC | last post by:
Hy! is it possible to pass function-arguments by reference? (for example in PHP you can use the "&" operator ... ) thx iolo
25
by: Steven Bethard | last post by:
So I end up writing code like this a fair bit: map = {} for key, value in sequence: map.setdefault(key, ).append(value) This code basically constructs a one-to-many mapping -- each value that...
0
by: Frans Englich | last post by:
Hello, I've stumbled over a behavior related to default function arguments which appears to be a bug, from what I can tell. See this snippet: >>> from kdecore import KURL >>> u = KURL(...
3
by: Ohmu | last post by:
Hi! How to pass an (multidimensional)array of something to a function with reference/pointer? Can anyone help me with that? Thanks, Ohmu
7
by: Amit Sharma | last post by:
Hi, I wanted to know when we call function with argument, Does the arguments stored in stack always or it compiler specific and could be stored in queue e.g. Does this program's output depends...
2
by: xideum | last post by:
Hi. Question: In a function that takes multiple arguments, each with a default value setis there anyway to either alter the order of the parameters you pass to it or skip the passing of certain...
21
by: dragoncoder | last post by:
Consider the following code. #include <stdio.h> int main() { int i =1; printf("%d ,%d ,%d\n",i,++i,i++); return 0; }
3
by: Rich | last post by:
Another noob question for you all . . . I have functions written in C++ that I want to call from C#. I need to be able to pass a reference to a value type (like an int) so that the function can...
2
by: svlsr2000 | last post by:
Why are function arguements pushed into stack and not heap or any registers. I know there may be a function which takes much more arguments then available registers:( Why can;t we use heap or some...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.