Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if $_GET is empty?

Tags:

php

get

How to check if $_GET is empty?

like image 957
Vamsi Krishna B Avatar asked Aug 04 '10 18:08

Vamsi Krishna B


4 Answers

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

like image 117
NikiC Avatar answered Oct 10 '22 16:10

NikiC


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 
} 
like image 25
sherilyn Avatar answered Oct 10 '22 16:10

sherilyn


Just to provide some variation here: You could check for

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

it is completely identical to testing $_GET.

like image 11
Pekka Avatar answered Oct 10 '22 17:10

Pekka


<?php
if (!isset($_GET) || empty($_GET))
{
    // do stuff here
}
like image 8
john010117 Avatar answered Oct 10 '22 18:10

john010117