$_GET

$HTTP_GET_VARS [非推奨]

(PHP 4 >= 4.1.0, PHP 5, PHP 7)

$_GET -- $HTTP_GET_VARS [非推奨]HTTP GET 変数

説明

URL パラメータで現在のスクリプトに渡された変数の連想配列です。

$HTTP_GET_VARS は同じ情報を持っていますが、 これはスーパーグローバルではありません (HTTP_GET_VARS$_GET は違う変数であり、PHPはそれぞれ別に扱います)。

変更履歴

バージョン 説明
4.1.0 $_GET が導入され、 $HTTP_GET_VARS は非推奨となりました。

例1 $_GET の例

<?php
echo 'Hello ' htmlspecialchars($_GET["name"]) . '!';
?>

ユーザーが http://example.com/?name=Hannes と入力したとします。

上の例の出力は、 たとえば以下のようになります。

Hello Hannes!

注意

注意:

これは 'スーパーグローバル' あるいは自動グローバル変数と呼ばれるものです。 スクリプト全体を通してすべてのスコープで使用することができます。 関数やメソッドの内部で使用する場合にも global $variable; とする必要はありません。

注意:

GET 変数は urldecode() 関数を介して渡されます。

add a note add a note

User Contributed Notes 4 notes

up
206
John Galt
9 years ago
Just a note, because I didn't know for sure until I tested it.

If you have a query string that contains a parameter but no value (not even an equals sign), like so:
http://path/to/script.php?a

The following script is a good test to determine how a is valued:
<pre>
<?php
print_r
($_GET);
if(
$_GET["a"] === "") echo "a is an empty string\n";
if(
$_GET["a"] === false) echo "a is false\n";
if(
$_GET["a"] === null) echo "a is null\n";
if(isset(
$_GET["a"])) echo "a is set\n";
if(!empty(
$_GET["a"])) echo "a is not empty";
?>
</pre>

I tested this with script.php?a, and it returned:

a is an empty string
a is set

So note that a parameter with no value associated with, even without an equals sign, is considered to be an empty string (""), isset() returns true for it, and it is considered empty, but not false or null. Seems obvious after the first test, but I just had to make sure.

Of course, if I do not include it in my browser query, the script returns
Array
(
)
a is null
up
9
timberspine _AT_ gmail _DOT_ com
11 years ago
Note that named anchors are not part of the query string and are never submitted by the browser to the server.

Eg.
http://www.xyz-abc.kz/index.php?title=apocalypse.php#doom

echo $_GET['title'];

// returns "apocalypse.php" and NOT "apocalypse.php#doom"

you would be better off treating the named anchor as another query string variable like so:
http://www.xyz-abc.kz/index.php?title=apocalypse.php&na=doom

...and then retrieve it using something like this:
$url = $_GET['title']."#".$_GET['na'];

Hope this helps someone...
up
4
chris at bjelleklang dot org
8 years ago
Please note that PHP setups with the suhosin patch installed will have a default limit of 512 characters for get parameters. Although bad practice, most browsers (including IE) supports URLs up to around 2000 characters, while Apache has a default of 8000.

To add support for long parameters with suhosin, add
suhosin.get.max_value_length = <limit> in php.ini
up
-14
whatchildisthis
1 year ago
The variable name $_GET is a bit misleading. It works with any HTTP request method that has a query component in the URI: GET, POST, PUT, PATCH, DELETE. A better name would be $_QUERY, similar to http_build_query and PHP_URL_QUERY in parse_url.
To Top