Home  >  Article  >  Backend Development  >  How to Access Multi-Valued Parameters in PHP $_GET Array?

How to Access Multi-Valued Parameters in PHP $_GET Array?

Patricia Arquette
Patricia ArquetteOriginal
2024-10-22 20:59:29605browse

How to Access Multi-Valued Parameters in PHP $_GET Array?

Accessing Multi-Valued Parameters in PHP $_GET Array

PHP's $_GET superglobal array allows access to query string parameters. By default, when multiple values are assigned to the same parameter, only the last value is stored in $_GET. However, it is possible to retrieve such values as an array.

Creating Multi-Valued $_GET Parameters

To send multiple values for a parameter in a query string, simply use the square bracket notation:

http://link/foo.php?id[]=1&id[]=2&id[]=3

Accessing the Array in PHP

Using the above technique, $_GET['id'] will become an array containing the multiple values:

<code class="php">echo $_GET['id'][0]; // Output: 1
echo $_GET['id'][1]; // Output: 2
echo $_GET['id'][2]; // Output: 3</code>

By accessing $_GET['id'] as an array, you can iterate over the values and process them individually or as a collection.

Note: This method is compatible with most web servers and browsers. However, it's worth noting that some older servers may not support multi-valued parameters.

The above is the detailed content of How to Access Multi-Valued Parameters in PHP $_GET Array?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn