Hi all,

I was wondering if it is a common thing to do when requesting variables to have the request names in an array and use a for loop to request the submitted variables. (will it hold the values if its a for loop?)

What i have at present is this:

$mobile_phone = $_REQUEST['mobile_phone'] ;
$mobile_phone = mysql_real_escape_string($mobile_phone);
$work_phone = $_REQUEST['work_phone'] ;
$work_phone = mysql_real_escape_string($work_phone);
$home_phone = $_REQUEST['home_phone'] ;
$home_phone = mysql_real_escape_string($home_phone);
$email_address = $_REQUEST['email_address'] ;
$email_address = mysql_real_escape_string($email_address);
$time = $_REQUEST['time'] ;
$time = mysql_real_escape_string($time);

and about ten other requests being made too by the way.

if i run it through an array and use a for loop request the variables, will it hold the values in the request to be used elsewhere in the script?

5 Years
Discussion Span
Last Post by gotboots

Everything will remain in $_REQUEST unless you specifically modify or remove it, allowing you to get the same information repeatedly from it.


cheers man for the quick reply,

so in essence (does this look right?):

$arr = array(mobile_phone, work_phone, home_phone, email_address, time);
foreach ($arr as &$value) 
    $value = $_REQUEST['$value'] ;
    $value = mysql_real_escape_string($value);

echo $mobile_phone;

If you have register_globals = on, you will automatically get variables that match your POST elements with appropriate values already.

If register_globals is off, you can do what you're doing with a couple changes

$arr = array("mobile_phone", "work_phone", "home_phone", "email_address", "time");
foreach ($arr as $value) // don't need the & if you're creating new separate variables 
    $$value = $_REQUEST[$value] ;
    $$value = mysql_real_escape_string($$value);
echo $mobile_phone;

Wicked LethargicPoster, that works. thanks so much, i can now take out about forty lines of code from each of my processing pages. Very happy indeed

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.