Jimmyt1988
Jimmyt1988

Reputation: 21136

PHP get ? without setting variable to anything

If I were to call:

www.*.com?hello

if($_GET["hello"]){
}

It will always return false because ?hello variable needs to be set as something...

Is there a way to get the ? part without setting the variable to anythign before hand?

Upvotes: 2

Views: 294

Answers (5)

verisimilitude
verisimilitude

Reputation: 5108

if(!empty($_GET["hello"]))
{

}

Instead of checking for both isset and $_GET != "".

Upvotes: 0

Vytautas
Vytautas

Reputation: 3539

You can check if variable is set like this:

if(isset($_GET["hello"])){

}

sometimes key_exists() is better because if $_GET["hello"] == null you can get false

if (key_exists("hello", $_GET)) {

}

Upvotes: 6

Vishal
Vishal

Reputation: 2181

The usual way is to check it like:

if(isset($_GET["hello"]) && $_GET["hello"] != ""){
  //code
}

Upvotes: 0

GolezTrol
GolezTrol

Reputation: 116110

Use array_key_exists:

if (array_key_exists("hello", $_GET)) {
}

Please read this for a difference between isset and array_key_exists.

Upvotes: 1

Martin.
Martin.

Reputation: 10529

$_GET["hello"] is falsy, check if it's set at all

if (isset($_GET["hello"])) {
   //do stuff
}

Upvotes: 1

Related Questions