Posted by peter on 06/15/07 13:36
>>
>>yup, treat $_POST as you would an array because it is an array
>>
>>example
>>foreach($_POST as $key => $item)
>>{
>>$$key = trim($_POST[$key]);
>>}
>>
>>key is a variable variable
>>
>>it would be the equivalent of this
>>
>>$apple = $_POST['apple'];
>>$orange = $_POST['orange'];
>>$pear = $_POST['pear'];
>>
>>etc..
>>
>>with the loop above you don't have to go thru the process of calling each
>>one out
>>it's done throu the loop - hope that makes sense
>>
>
> Thank you. I've never worked with foreach (Shock! Horror!) so it'll
> be interesting to see how I get along with it.
Do be weary of doing it exactly like that. you could inadvertantly overwrite
important variables. It would be a better option maybo to put them into a
new array much like:-
$clean = array();
foreach($_POST as $key => $item)
{
$clean[$key] = trim($item);
}
(there was no need to use the $_POST array within the foreach that the other
op provided)
if you do it the other way and you rely on say a variable called $logged_in
it would be easy for someone to inadvertantly overwrite that imporant
variable. Something else you could do is create a recursive function in case
there are arrays within the $_POST array.
[Back to original message]
|