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

Linda Hamilton
Release: 2024-10-22 18:29:24
Original
504 people have browsed it

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
Copy after login
Copy after login

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>
Copy after login

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>
Copy after login

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

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

The script will output the following array:

Array
(
    [0] => 1
    [1] => 2
    [2] => 3
)
Copy after login

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!

source:php
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template