php 如何检查 $_GET 是否为空?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3408616/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:38:43  来源:igfitidea点击:

How to check if $_GET is empty?

phpget

提问by Vamsi Krishna B

How to check if $_GETis empty?

如何检查是否$_GET为空?

回答by NikiC

You said it yourself, check that it's empty:

你自己说的,检查它是empty

if (empty($_GET)) {
    // no data passed by get
}

See, PHP is so straightforward. You may simply write, what you think ;)

看,PHP 是如此简单。你可以简单地写下你的想法;)

This method is quite secure. !$_GETcould give you an undefined variable E_NOTICE if $_GETwas unset (not probable, but possible).

这种方法非常安全。!$_GET如果$_GET未设置(不太可能,但可能),可能会给你一个未定义的变量 E_NOTICE 。

回答by sherilyn

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 
} 

回答by Pekka

Just to provide some variation here: You could check for

只是在这里提供一些变化:您可以检查

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

it is completely identical to testing $_GET.

它与测试完全相同$_GET

回答by john010117

<?php
if (!isset($_GET) || empty($_GET))
{
    // do stuff here
}

回答by Your Common Sense

if (!$_GET) echo "empty";

why do you need such a checking?

为什么需要这样的检查?

lol
you guys too direct-minded.
don't take as offense but sometimes not-minded at all
$_GET is very special variable, not like others.
it is supposed to be always set. no need to treat it as other variables. when $_GET is not set and it's expected - it is emergencycase and that's what "Undefined variable" notice invented for

大声笑
你们太直接了。
不要冒犯但有时根本不介意
$_GET 是非常特殊的变量,不像其他变量。
它应该总是被设置。无需将其视为其他变量。当 $_GET 未设置并且是预期的 - 这是紧急情况,这就是发明的“未定义变量”通知

回答by Martin Bean

Easy.

简单。

if (empty($_GET)) {
    // $_GET is empty
}

回答by Martin Bean

Here are 3 different methods to check this

这里有 3 种不同的方法来检查这个

<?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";
?>

回答by vlad b.

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

我将使用以下 if 语句,因为它更易于阅读(并在将来进行修改)


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...
}