What does $_GET['key'] return if the key i

2019-06-17 07:15发布

What does $_GET return when the index is not set? (Couldn't find anything in php manual about $_GET.)

I wrote this to check, if the $_GET['id'] isset - and if it is not, set $id to false:

<?php $id = (isset($_GET['id'])) ? $_GET['id'] : false ?>

标签: php arrays get
6条回答
SAY GOODBYE
2楼-- · 2019-06-17 07:48

There's an example in the manual: PHP: $_GET.

The relevant part of the example script is:

if(isset($_GET["a"])) echo "a is set\n";

That part of the script prints out "a is set" when a is passed as a parameter through the URL. http://path/to/script.php?a

查看更多
老娘就宠你
3楼-- · 2019-06-17 07:56

Unset indexes have the value NULL. Accessing them will result in a notice being raised (unless your error level is set to swallow notices).

查看更多
姐就是有狂的资本
4楼-- · 2019-06-17 08:02
var_dump($_GET['nonexistent']); // outputs NULL

http://php.net/manual/en/function.var-dump.php

查看更多
地球回转人心会变
5楼-- · 2019-06-17 08:03

$_GET is just an ordinary array, so it behaves exactly the same as any other array.

This means, it will return NULL to the variable and raise the "undefined index" notice when you call a non-existing index.

The only thing you need to be aware of with $_GET is that is contains unsafe (user-modifiable) data

查看更多
看我几分像从前
6楼-- · 2019-06-17 08:03

If the index is not set, running isset() on that index within $_GET returns false.

查看更多
混吃等死
7楼-- · 2019-06-17 08:11

$_GET is a superglobal array:

As such, it follows the rules for accessing array keys:

Attempting to access an array key which has not been defined is the same as accessing any other undefined variable: an E_NOTICE-level error message will be issued, and the result will be NULL.

查看更多
登录 后发表回答