2

Say your HTTP request is formatted like this:

GET /path/to/file.php?var[]=1&var[]=2 

PHP will populate that into an array named $_GET: array('var' => array(1, 2))

My question is... is this a PHP thing or is this behavior governed by an RFC? How would a web accessible node.js or Python script deal with this?

4
  • 1
    It's language-specific. Commented Jul 7, 2014 at 14:50
  • 1
    I'm going to guess that it's a PHP thing, on the basis that ?foo.bar=baz gets converted to $_GET['foo_bar'] = 'baz';. Other server-side languages may provide similar features though. Commented Jul 7, 2014 at 14:52
  • @Niet the Dark Absol - that's interesting. I didn't know that! Commented Jul 7, 2014 at 15:05
  • 1
    @neubert Heh, yeah, I found it out the hard way when working with <input type="image" /> coordinates ;) They get sent as name.x and name.y but PHP exposes them as name_x and name_y. I'm guessing this is a leftover from when "register globals" was a default thing. Commented Jul 7, 2014 at 15:07

2 Answers 2

2

PHP by default will overwrite previous values, and you end up with the LAST key:value pair found in the query string. But in PHP you can use the [] array as fieldname hack:

  1. example.com?foo=bar&foo=baz&foo=qux
  2. example.com?foo[]=bar&foo[]=baz&foo[]=qux

Line #1 produces $_GET like this:

$_GET = array( 'foo' => 'qux' ); 

Line #2 produces

$_GET['array'] = array 'foo' => array('bar', 'baz', 'qux'); } 

Note that this behavior is PHP specific. Other languages do their own thing. As far as I remember, Perl by default will keep all values as an array.

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

Comments

1

This is a specific feature of PHP with no related RFCs or other specs. I can't find any specific statements to that effect, but reading this FAQ Q/A seems to imply the same:

http://docs.php.net/manual/en/faq.html.php#faq.html.arrays

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.