Variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Jim_Bo
Forum Contributor
Posts: 390
Joined: Sat Oct 02, 2004 3:04 pm

Variables

Post by Jim_Bo »

Hi,

Is the following code bad practice:

Code: Select all

foreach($_POST as $key=>$value){
$$key = ValidateOutput($value);
}
allowing you to call on the variables with just:

$variable

rather than:

Code: Select all

foreach($_POST as $key=>$value){
$_POST[$key] = (ValidateOutput($value));
}
and calling the variables as:

$_POST['variable']


?


Thanks
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post by Christopher »

They are both essentially the same, so it depends on other factors which one you might use. The first extracts the values into the current scope which is what some template systems do. The second overwrites the original values which prevents you from accessing them -- which can be good and bad depending on need. And is it not clear what your ValidateOutput() function does. Usually you validate input based on specific rules for each variable, and then escape output depending on destination.
(#10850)
Post Reply