How to check if $_GET is empty?

2019-01-07 10:19发布

How to check if $_GET is empty?

标签: php get
8条回答
冷血范
2楼-- · 2019-01-07 10:49

Easy.

if (empty($_GET)) {
    // $_GET is empty
}
查看更多
放荡不羁爱自由
3楼-- · 2019-01-07 10:51

I would use the following if statement because it is easier to read (and modify in the future)


if(!isset($_GET) || !is_array($_GET) || count($_GET)==0) {
   // empty, let's make sure it's an empty array for further reference
   $_GET=array();
   // or unset it 
   // or set it to null
   // etc...
}
查看更多
Animai°情兽
4楼-- · 2019-01-07 10:55

Just to provide some variation here: You could check for

if ($_SERVER["QUERY_STRING"] == null)

it is completely identical to testing $_GET.

查看更多
老娘就宠你
5楼-- · 2019-01-07 11:01

Here are 3 different methods to check this

<?php
//Method 1
if(!empty($_GET))
echo "exist";
else
echo "do not exist";
//Method 2
echo "<br>";
if($_GET)
echo "exist";
else
echo "do not exist";
//Method 3
if(count($_GET))
echo "exist";
else
echo "do not exist";
?>
查看更多
相关推荐>>
6楼-- · 2019-01-07 11:02

i guess the simplest way which doesn't require any operators is

if($_GET){
//do something if $_GET is set 
} 
if(!$_GET){
//do something if $_GET is NOT set 
} 
查看更多
等我变得足够好
7楼-- · 2019-01-07 11:04
<?php
if (!isset($_GET) || empty($_GET))
{
    // do stuff here
}
查看更多
登录 后发表回答