Reputation: 49
on an PHP POST, is there a way of automatically setting each POST variable to its own named variable?
For example if I post
name = "henry"
age = "20"
location = "earth"
Instead of doing:
$name = $_POST['name'];
$age = $_POST['age'];
$location = $_POST['location'];
is there a way of looping through all POST variables and setting it to the same named standard variable?
Upvotes: 1
Views: 682
Reputation: 245
You can be use this:
if ($_POST) {
foreach ($_POST as $key => $value) {
$name = "{$key}";
$$name = $value;
echo "<pre>";
echo $name;
}
echo "<pre>";
echo $name;
echo "<pre>";
echo $age;
echo "<pre>";
echo $location;
}
Upvotes: 0
Reputation: 11
It is not recommended to set $_POST variables programmatically. However, if you want it you can use extract function
extract($_POST,EXTR_OVERWRITE,'prefix');
Upvotes: 1
Reputation: 313
Extract function can help with this issue:
$_POST = ['foo' => 'bar'];
extract($_POST);
var_dump($foo) //returns 'bar'
http://php.net/manual/en/function.extract.php
Upvotes: 3