Reputation: 309
I am setting up an array with $key => $value. I am using this to set up a report from a form submission. I am trying to set a php variable inside the foreach loop and use it outside the loop; however, it isn't working. Any ideas why?
$formfields = array(
"Company Name" => "company",
"Contact Name" => "name",
"Address" => "address",
"City" => "city",
"State" => "state",
"Zip Code" => "zip",
"Phone Number" => "phone",
"Brand of Tool" => "brand",
"Tool Model" => "model",
"Description of Problem" => "description",
"Repair or Rebuild" => "repairorrebuild",
"Estimate or Repair & Return" => "estimateorrepair"
);
foreach ($formfields as $key => $value) {
'$'. $value = htmlspecialchars($_POST[$value]);
}
echo $company .' '. $name;
I've tested by echoing the value's inside the foreach loop and they do return the values from the form. So I know there are no typo's.
Upvotes: 1
Views: 1017
Reputation: 12236
To make dynamic variables you should use {}
foreach ($formfields as $key => $value) {
${$value} = htmlspecialchars($_POST[$value]);
}
Upvotes: 2