I have a selection of post fields which are submitted through a web form:
$_POST['first_name'];
$_POST['last_name'];
$_POST['house_number'];
$_POST['postcode'];
etc
I would like to add the following code structure to each one:
if (isset($_POST['first_name'])) {
$first_name = mysql_prep($_POST['first_name']);
}
How can I use a loop to achieve this to save having to repeat myself multiple times?
Thanks
You can use PHP’s
extract()function to do this for you.If you only want a certain list of keys, and not the full array, you can also use
variable-variables(a variable with two leading$, such as$$var):