How to check if $_GET is empty?

PhpGet

Php Problem Overview


How to check if $_GET is empty?

Php Solutions


Solution 1 - Php

You said it yourself, check that it's empty:

if (empty($_GET)) {
    // no data passed by get
}

See, PHP is so straightforward. You may simply write, what you think ;)

This method is quite secure. !$_GET could give you an undefined variable E_NOTICE if $_GET was unset (not probable, but possible).

Solution 2 - Php

i guess the simplest way which doesn't require any operators is

if($_GET){
//do something if $_GET is set 
} 
if(!$_GET){
//do something if $_GET is NOT set 
} 
 

Solution 3 - Php

Just to provide some variation here: You could check for

if ($_SERVER["QUERY_STRING"] == null)

it is completely identical to testing $_GET.

Solution 4 - Php

<?php
if (!isset($_GET) || empty($_GET))
{
    // do stuff here
}

Solution 5 - Php

if (!$_GET) echo "empty";

why do you need such a checking?

lol
you guys too direct-minded.
don't take as offense but sometimes not-minded at all
$_GET is very special variable, not like others.
it is supposed to be always set. no need to treat it as other variables. when $_GET is not set and it's expected - it is emergency case and that's what "Undefined variable" notice invented for

Solution 6 - Php

Easy.

if (empty($_GET)) {
    // $_GET is empty
}

Solution 7 - Php

I would use the following if statement because it is easier to read (and modify in the future)


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...
}

Solution 8 - Php

Here are 3 different methods to check this

<?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";
?>

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionVamsi Krishna BView Question on Stackoverflow
Solution 1 - PhpNikiCView Answer on Stackoverflow
Solution 2 - PhpsherilynView Answer on Stackoverflow
Solution 3 - PhpPekkaView Answer on Stackoverflow
Solution 4 - Phpjohn010117View Answer on Stackoverflow
Solution 5 - PhpYour Common SenseView Answer on Stackoverflow
Solution 6 - PhpMartin BeanView Answer on Stackoverflow
Solution 7 - Phpvlad b.View Answer on Stackoverflow
Solution 8 - Phpuser2541787View Answer on Stackoverflow