php 如何检查 $_GET 参数是否存在但没有值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12503364/
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
How do I check if a $_GET parameter exists but has no value?
提问by Ben
I want to check if the appparameter exists in the URL, but has no value.
我想检查该app参数是否存在于 URL 中,但没有值。
Example:
例子:
my_url.php?app
I tried isset()and empty(), but don't work. I've seen it done before and I forgot how.
我试过isset()和empty(),但不起作用。我以前见过它完成,我忘记了怎么做。
回答by Jay Hewitt
Empty is correct. You want to use both is set and empty together
空是正确的。你想同时使用 set 和 empty
if(isset($_GET['app']) && !empty($_GET['app'])){
echo "App = ".$_GET['app'];
} else {
echo "App is empty";
}
回答by Kermit
emptyshould be working (if(empty($_GET[var]))...) as it checks the following:
empty应该正常工作 ( if(empty($_GET[var]))...),因为它会检查以下内容:
The following things are considered to be empty:
"" (an empty string) 0 (0 as an integer) 0.0 (0 as a float) "0" (0 as a string) NULL FALSE array() (an empty array) $var; (a variable declared, but without a value)
以下内容被认为是空的:
"" (an empty string) 0 (0 as an integer) 0.0 (0 as a float) "0" (0 as a string) NULL FALSE array() (an empty array) $var; (a variable declared, but without a value)
Here are your alternatives:
以下是您的替代方案:
is_null- Finds whether a variable is NULL
is_null- 查找变量是否为 NULL
if(is_null($_GET[var])) ...
defined- Checks whether a given named constant exists
defined- 检查给定的命名常量是否存在
if(defined($_GET[var])) ...
回答by Ali
You can simply check that byarray_key_exists('param', $_GET);.
您可以简单地通过array_key_exists('param', $_GET);.
Imagine this is your URL: http://example.com/file.php?param. It has the paramquery parameter, but it has not value. So its value would be nullactually.
想象一下这是您的 URL:http://example.com/file.php?param。它有param查询参数,但没有值。所以它的价值null实际上是。
array_key_exists('param', $_GET);returns trueif paramexists; returns falseif it doesn't exist at all.
array_key_exists('param', $_GET);true如果param存在则返回;false如果它根本不存在,则返回。
回答by 84em
if( isset($_GET['app']) && $_GET['app'] == "")
{
}

