Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php change post data to variable

I'm trying to change given $_POST data to variable format, here is my code:

<?php
$_POST["abc"]=2;
$post = array("abc");

foreach($post as $field) {
    global $field;
    $field = $_POST[$field];
}

echo $abc;
?>

but when I'm trying to run this code, I only get:

PHP Notice: Undefined variable: abc in /var/www/ on line 8

How can I change chosen POST variables in loop to normal variables? (eg. $_POST["abc"] to $abc)

like image 757
Jakub Pastuszuk Avatar asked Sep 16 '26 18:09

Jakub Pastuszuk


2 Answers

Use the extract function

extract($_POST)
like image 180
Hirnhamster Avatar answered Sep 19 '26 09:09

Hirnhamster


You can tried with PHP variable variable.

<?php
$_POST["abc"]=2;
$post = array("abc");

foreach($post as $field) {
     global $$field;
     $$field = $_POST[$field];
}

echo $abc;

?>
like image 41
Dmitry Bezik Avatar answered Sep 19 '26 08:09

Dmitry Bezik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!