2

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

9
  • You want to store the posted values in separate variabels? You have to write it in the oposite direction: $name = $_POST['name']; Commented Apr 13, 2011 at 19:46
  • 1
    The error is on the previous line from the first $_POST. Update with that code please. Commented Apr 13, 2011 at 19:47
  • 1
    Why would be assigning variables to the post array? Not sure what you are trying to do here. Commented Apr 13, 2011 at 19:47
  • 1
    @all Has anyone actually read the error? The compiler is telling him he put a variable right smack in the middle of something else. That variable is the first $_POST. Doesn't matter if he does a=b or b=a. The error is unrelated to your solutions. Commented Apr 13, 2011 at 19:53
  • 1
    @Khez: It's probably also useful to do this for unit testing. Commented Apr 13, 2011 at 20:15

5 Answers 5

3

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.

Sign up to request clarification or add additional context in comments.

2 Comments

@Khez: Oddly, I was responding to your post-level comment while you were responding to my answer.
Thanks so much, that was it a simple ; missing in the previous line. I also inverted to the A = B order that everybody stated.
1

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.

Comments

1

go the other way:

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

Comments

1

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.

Comments

0

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.

2 Comments

Thanks but I'm still getting the same error at: $name = $_POST['name']; Parse error: syntax error, unexpected T_VARIABLE
@Gabriel check @AgentConundrum's response or my comment in your OP.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.