Php 简明教程
PHP - $_GET
$_GET 是 PHP 中的超级全局变量之一。它是一个关联数组,其中包含通过附加到 HTTP 请求 URL 的查询字符串传递给当前脚本的变量。请注意,除了 GET 请求外,所有带有查询字符串的请求都会填充此数组。
$_GET is one of the superglobals in PHP. It is an associative array of variables passed to the current script via the query string appended to the URL of HTTP request. Note that the array is populated by all requests with a query string in addition to GET requests.
$HTTP_GET_VARS 包含相同的基本信息,但它现在已被弃用。
$HTTP_GET_VARS contains the same initial information, but that has now been deprecated.
默认情况下,客户端浏览器通过使用 HTTP GET 方法向服务器上的 URL 发送请求。附加到 URL 的查询字符串可能包含由“&”符号连接的键值对。$_GET 关联数组存储这些键值对。
By default, the client browser sends a request for the URL on the server by using the HTTP GET method. A query string attached to the URL may contain key value pairs concatenated by the "&" symbol. The $_GET associative array stores these key value pairs.
将以下脚本保存在 Apache 服务器的文档文件夹中。如果您在 Windows 上使用 XAMPP 服务器,请将脚本作为“hello.php”放置在“c:/xampp/htdocs”文件夹中。
Save the following script in the document folder of Apache server. If you are using XAMPP server on Windows, place the script as "hello.php" in the "c:/xampp/htdocs" folder.
<?php
echo "<h3>First Name: " . $_REQUEST['first_name'] . "<br />" .
"Last Name: " . $_REQUEST['last_name'] . "</h3>";
?>
启动 XAMPP 服务器,并在浏览器窗口中输入“http://localhost/hello.php?first_name=Mukesh&last_name=Sinha”作为 URL。您应该获得以下 output −
Start the XAMPP server, and enter "http://localhost/hello.php?first_name=Mukesh&last_name=Sinha" as the URL in a browser window. You should get the following output −
当 HTML 表单数据被提交到带有 GET 动作的 URL 时,$_GET 数组也会被填充。
The $_GET array is also populated when a HTML form data is submitted to a URL with GET action.
在文档根目录下,将以下脚本保存为“ hello.html ”−
Under the document root, save the following script as "hello.html" −
<html>
<body>
<form action="hello.php" method="get">
<p>First Name: <input type="text" name="first_name"/></p>
<p>Last Name: <input type="text" name="last_name" /></p>
<input type="submit" value="Submit" />
</form>
</body>
</html>
在您的浏览器中,输入 URL "http://localhost/hello.html" −
In your browser, enter the URL "http://localhost/hello.html" −
您应该在浏览器窗口中获得类似的 output −
You should get a similar output in the browser window −
在以下示例中,htmlspecialchars() 用于将字符转换为 HTML 实体 −
In the following example, htmlspecialchars() is used to convert characters in HTML entities −
Character |
Replacement |
& (ampersand) |
& |
" (double quote) |
" |
' (single quote) |
' or ' |
< (less than) |
< |
> (greater than) |
> |
假设浏览器中的 URL 为“http://localhost/hello.php?name=Suraj&age=20”−
Assuming that the URL in the browser is "http://localhost/hello.php?name=Suraj&age=20" −
<?php
echo "Name: " . htmlspecialchars($_GET["name"]) . "";
echo "Age: " . htmlspecialchars($_GET["age"]) . "<br/>";
?>
它将生成以下 output −
It will produce the following output −
Name: Suraj
Age: 20