Gabriel Meono
Gabriel Meono

Reputation: 1010

PHP: Make an Array value into a variable

I´m making a form, and I want to change the value of the POST into a variable. But I'm doing it wrong somehow.

Check the sample:

$_POST['name'] = $name;
$_POST['age'] = $age;
$_POST['country'] = $country;

This error pops: Parse error: syntax error, unexpected T_VARIABLE on the first $_POST

Upvotes: 2

Views: 314

Answers (5)

anon
anon

Reputation:

While everyone else is entirely correct to point that you shouldn't be assigning values to the $_POST superglobal, it is possible for you to make such an assignment. The $_POST superglobal is just an array, after all, and so it acts like one.

The error you're seeing is because PHP is recognizing $_POST['name'] as being part of the previous statement. Check to make sure that you have properly ended the previous statement (i.e. the line before $_POST['name'] = $name ends with a ;).

You probably do want to be assigning $_POST['name'] to a variable, rather than the other way around as you have it now, but that's not what's causing the error.

Upvotes: 3

DaOgre
DaOgre

Reputation: 2100

You have this backwards, it should be:

$name = $_POST["name"];  
$age= $_POST["age"];  
$country= $_POST["country"];  

The value to the right of the = gets assigned to the left. As is you are trying to replace the post variable with an unassigned variable.

Upvotes: 0

halfdan
halfdan

Reputation: 34254

Assignment works right to left, so to get the values from the into a variable you'd have to do:

$name = $_POST['name'];
...

Your code above does not contain any syntax error, it must be from somewhere else.

Upvotes: 1

Naftali
Naftali

Reputation: 146360

go the other way:

$name = $_POST['name'];
$age = $_POST['age'];
$country = $_POST['country'];

Upvotes: 1

Mike Lewis
Mike Lewis

Reputation: 64177

You don't programmatically set $_POST variables. These are set by the server based on what was POST'ed to that page(via forms or otherwise).

So I'm fairly sure you want:

$name = $_POST['name'];
$age = $_POST['age'];
$country = $_POST['country'];

This is because the assignment operator works as such:

a = b

Set a to the value of b.

Upvotes: 1

Related Questions