如何检查
$_GET
是否为空?你自己说了,检查它是否为empty
:
if (empty($_GET)) {
// no data passed by get
}
看,PHP就是这么简单直接。你可以随意写出你想要的代码 ;)
这种方法非常安全。!$_GET
如果$_GET
未设置(可能性不大,但有可能)会导致未定义变量E_NOTICE。
我想最简单的方法是不需要任何运算符的:
if($_GET){
//do something if $_GET is set
}
if(!$_GET){
//do something if $_GET is NOT set
}
为了提供一些变化,您可以检查
if ($_SERVER["QUERY_STRING"] == null)
它与测试$_GET
完全相同。
!isset
、null
还是''
?null
在这里可行吗?你测试过吗? - NikiCarray_key_exists()
检查。 - Pekka<?php
if (!isset($_GET) || empty($_GET))
{
// do stuff here
}
!isset($_GET)
,if (empty($_GET))
就足够了。 - Alexander Konstantinovempty
简单地检查变量是否设置,因此使用isset
进行检查是多余的。 - NikiCempty
确实是一种语言结构。 - Wrikkenif (!$_GET) echo "empty";
为什么需要这样的检查?
哈哈
你们太直接了。
不要生气,但有时候一点也不灵活
$_GET是非常特殊的变量,不像其他变量。
它应该始终被设置。不需要像其他变量一样对待它。
当$_GET未设置且预期设置时,这是一种紧急情况,这就是“未定义变量”通知发明的原因。
简单易懂。
if (empty($_GET)) {
// $_GET is empty
}
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...
}
empty()
返回TRUE
。 - apadernotrim()
函数来处理$_GET
和$_POST
变量。有时候,空格可能会被误认为是值的一部分,而实际上并不是。 - sanusart<?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";
?>