15

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.

Lypyrhythm
  • 324
  • 2
  • 13
Ben
  • 5,627
  • 9
  • 35
  • 49

4 Answers4

34

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";
}
Jay Hewitt
  • 1,116
  • 8
  • 16
  • 1
    Your "App is empty" will display if it's not set in the first place, I'd put an elseif statement in there – Scuzzy Sep 19 '12 at 22:28
  • 5
    Indeed, a more accurate statement would be: if(isset($_GET['app']) && !empty($_GET['app'])){ echo "App = ".$_GET['app']; } elseif(isset($_GET['app'])) { echo "App is empty"; } else { echo "App is not set"; } – Jay Hewitt Sep 20 '12 at 13:00
5

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])) ...
Kermit
  • 33,827
  • 13
  • 85
  • 121
2
if( isset($_GET['app']) && $_GET['app'] == "")
{

}
sachleen
  • 30,730
  • 8
  • 78
  • 73
84em
  • 41
  • 2
2

You can simply check that by array_key_exists('param', $_GET);.

Imagine this is your URL: http://example.com/file.php?param. It has the param query parameter, but it has not value. So its value would be null actually.

array_key_exists('param', $_GET); returns true if param exists; returns false if it doesn't exist at all.

Ali
  • 1,268
  • 1
  • 13
  • 20