Home  >  Article  >  Backend Development  >  How to Pass and Access Array Values in PHP $_GET?

How to Pass and Access Array Values in PHP $_GET?

Linda Hamilton
Linda HamiltonOriginal
2024-10-22 18:29:24426browse

How to Pass and Access Array Values in PHP $_GET?

PHP $_GET Array as an Array

The $_GET array in PHP is a superglobal variable that can be used to obtain information sent via a URL query string. Typically, each key in the array represents a variable name, while the corresponding value contains the associated value. By default, the $_GET values are treated as strings.

Sending an Array Value in $_GET

However, it is possible to pass an array value in the $_GET query string. To do this, you need to use the following syntax:

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

In this case, the "id" parameter becomes an array with three elements, each containing one of the values provided in the query string.

Accessing the Array Value in PHP

On the PHP side, you can access the array value using the following syntax:

<code class="php">$_GET['id'];</code>

This will return an array containing the three values that were passed in the query string.

Example

Consider the following PHP script:

<code class="php"><?php

if (isset($_GET['id'])) {
  print_r($_GET['id']);
}

?></code>

If you access this script via a URL like the following:

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

The script will output the following array:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)

The above is the detailed content of How to Pass and Access Array Values in PHP $_GET?. 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