Form input field names containing square brackets like field[index]

What is the mechanism behind? At which
point this names that merely contain
brackets are converted to arrays? Is
this a feature of the HTPP protocol?
Of web servers? Of the PHP language?>

This is a feature of the PHP language. In fact, the HTTP protocol does not forbid the use of multiple identical GET/POST parameters. According to the HTTP spec, the following:

foo=bar&foo=baz

Should not result in foo == baz. These are two parameters with two different values. However, PHP will overwrite the former foo with the latest, resulting in $_POST['foo'] == 'baz', even if they could be parsed separately.

Continuing the previous question, is
this a commonly used hack or a normal
programming tool?

It depends on the point of view. In the PHP world, it is completely normal, as the language does not support the specification of multiple parameters of the same name without using the brackets []. In the HTTP world though, foo != foo[].

What are (all) the rules of using
brackets in input field names?

The same as PHP arrays, except that you don’t have to quote string keys.

Can multidimensional arrays be created
this way?

Yes, you can.

Leave a Comment