PHP:将Array值转换为变量
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
我正在创建一个表单,我想将POST的值更改为变量。 但是我以某种方式做错了。 p>
检查示例: p>
$ _ POST ['name'] = $ name;
$ _POST ['age'] = $ age;
$ _POST ['country'] = $ country;
code> pre>
此错误弹出: 第一个$ _POST p>
div>上的解析错误:语法错误,意外的T_VARIABLE code>
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.
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.
go the other way:
$name = $_POST['name'];
$age = $_POST['age'];
$country = $_POST['country'];
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.
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.