note 87650 added to reserved.variables.post
| From: | james dot ellis at gmail dot com | Date: | Mon, 15 Dec 2008 01:38:45 +0000 |
| Subject: | note 87650 added to reserved.variables.post | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-148163@lists.php.net to get a copy of this message | ||
One feature of PHP's processing of POST and GET variables is that it automatically decodes
indexed form variable names.
I've seem innumerable projects that jump through extra & un-needed processing hoops to
decode variables when PHP does it all for you:
Example pseudo code:
Many web sites do this:
<form ....>
<input name="person_0_first_name" value="john" />
<input name="person_0_last_name" value="smith" />
...
<input name="person_1_first_name" value="jane" />
<input name="person_1_last_name" value="jones" />
</form>
When they could do this:
<form ....>
<input name="person[0][first_name]" value="john" />
<input name="person[0][last_name]" value="smith" />
...
<input name="person[1][first_name]" value="jane" />
<input name="person[1][last_name]" value="jones" />
</form>
With the first example you'd have to do string parsing / regexes to get the correct values out
so they can be married with other data in your app... whereas with the second example.. you will end
up with something like:
<?php
var_dump($_POST['person']);
//will get you something like:
array (
0 =>
array('first_name'=>'john','last_name'=>'smith'),
1 =>
array('first_name'=>'jane','last_name'=>'jones'),
)
?>
This is invaluable when you want to link various posted form data to other hashes on the server
side, when you need to store posted data in separate "compartment" arrays or when you want
to link your POSTed data into different record handlers in various Frameworks.
Remember also that using [] as in index will cause a sequential numeric array to be created once the
data is posted, so sometimes it's better to define your indexes explicitly.
----
Server IP: 117.55.229.1
Probable Submitter: 58.6.148.157
----
Manual Page -- http://www.php.net/manual/en/reserved.variables.post.php
Edit -- https://master.php.net/note/edit/87650
Del: integrated -- https://master.php.net/note/delete/87650/integrated
Del: useless -- https://master.php.net/note/delete/87650/useless
Del: bad code -- https://master.php.net/note/delete/87650/bad+code
Del: spam -- https://master.php.net/note/delete/87650/spam
Del: non-english -- https://master.php.net/note/delete/87650/non-english
Del: in docs -- https://master.php.net/note/delete/87650/in+docs
Del: other reasons-- https://master.php.net/note/delete/87650
Reject -- https://master.php.net/note/reject/87650
Search -- https://master.php.net/manage/user-notes.php