2

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?

5 Answers 5

6

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

}
Sign up to request clarification or add additional context in comments.

2 Comments

You are a hero.. I am a dumb ass.
@JamesT upvote on your comment for the self depricating humor.
1

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

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

Comments

1

Use array_key_exists:

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

Please read this for a difference between isset and array_key_exists.

Comments

0

The usual way is to check it like:

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

Comments

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

}

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.