Getting confused with empty, isset, !empty, !isset
I have the following which doesn't work properly as $_GET['category']
can also equal 0.
if ( empty( $_GET['category'] ) ){
// do something
} else {
// do something else
}
How do I re-write this if statement to do 3 different things
$_GET['category']
does not exist at all $_GET['category'] == 0
$_GET['category'] ==
something other than "does not exist" and 0. I've tried different combinations of empty()
, !empty()
, isset()
, !isset()
but I'm not getting the results I'm after. I'm probably doing the wrong combinations...
This isn't really hard to do: isset
is the method you need.
if (!isset($_GET['category'])) {
// category isn't set
} elseif ($_GET['category'] === '0') {
// category is set to 0
} else {
// category is set to something other than 0
}
Note that I have compared for exact equality to the string '0'
, because GET
and POST
variables are always strings (or occasionally arrays) and never numbers when PHP first receives them.
if (!isset($_GET['category']))
{
1. Do something if $_GET['category'] does not exist at all
}
elseif ($_GET['category'] == 0)
{
2. Do something if $_GET['category'] == 0
}
elseif (isset($_GET['category']) && ($_GET['category'] != 0)
{
3. Do something if $_GET['category'] == something other than "does not exist" and 0.
}
我的括号可能稍微有些偏差,但希望能帮助你。
有这种情况下的filter_input
功能。
上一篇: 对于CodeMash 2012的“Wat”演讲中提到的这些奇怪的JavaScript行为,有什么解释?
下一篇: 混淆空,isset,!空,!isset