How to get parameters from URL in php

下次还敢
Release: 2024-04-29 10:42:12
Original
322 people have browsed it

To obtain parameters from the URL in PHP, you can use the $_GET super global variable: Make sure the request method is GET. Use the $_GET array to access parameters: keys are parameter names and values ​​are parameter values.

How to get parameters from URL in php

How to get parameters from URL in PHP

In order to get parameters from URL in PHP, you You can use the $_GET superglobal variable.

Steps:

  1. Determine the request method: First, check whether the request method is GET. You can use $_SERVER['REQUEST_METHOD'] to get the request method.
  2. Access parameters: If the request method is GET, you can use the $_GET array to access the parameters in the query string. $_GET The array is an associative array where the keys are parameter names and the values ​​are parameter values.

Example:

The following example demonstrates how to retrieve a file from the URL https://example.com/index.php?name=John&age=30 Get parameters in:

<code class="php"><?php
// 检查请求方法
if ($_SERVER['REQUEST_METHOD'] == 'GET') {
    // 获取参数
    $name = $_GET['name'];
    $age = $_GET['age'];

    // 输出参数值
    echo "姓名:$name" . PHP_EOL;
    echo "年龄:$age" . PHP_EOL;
}
?></code>
Copy after login

Output:

<code>姓名:John
年龄:30</code>
Copy after login

The above is the detailed content of How to get parameters from URL in php. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!