Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I check if a $_GET parameter exists but has no value?

I want to check if the app parameter exists in the URL, but has no value.

Example:

my_url.php?app

I tried isset() and empty(), but don’t work. I’ve seen it done before and I forgot how.

like image 778
Ben Avatar asked Sep 19 '12 21:09

Ben


2 Answers

Empty is correct. You want to use both is set and empty together

if(isset($_GET['app']) && !empty($_GET['app'])){
    echo "App = ".$_GET['app'];
} else {
    echo "App is empty";
}
like image 139
Jay Hewitt Avatar answered Sep 18 '22 05:09

Jay Hewitt


empty should be working (if(empty($_GET[var]))...) as it checks the following:

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)

Here are your alternatives:

is_null - Finds whether a variable is NULL

if(is_null($_GET[var])) ...

defined - Checks whether a given named constant exists

if(defined($_GET[var])) ...
like image 29
Kermit Avatar answered Sep 21 '22 05:09

Kermit