Jump to content

What does $_GET return with unknown variable?

- - - - -

  • Please log in to reply
2 replies to this topic

#1
jimiwa

jimiwa

    Newbie

  • Members
  • PipPip
  • 14 posts
Hi,
I'm wondering what the $_GET function returns when the paramater
checking for being passed has not been declared or listed in the
address bar address entered.
For example
$idd = $_GET['id'];

what would the value of $idd be if in the address bar:
index.php?var1=5&var2=6

id=val
is not found ?

#2
Vaielab

Vaielab

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 547 posts
return nothing (well it return null)
so you should always check is the variable exist with
if (isset($_GET['id'])) {

...

}


#3
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
  • Location:Vancouver, Eh! Cleverness: 200
Yes, $_GET (in the appropriate CGI context) will always be an array with zero elements, so it does exist. However if you call a non-existent array index then it will return NULL (which in PHP is considered to be not set)

You can always verify these qualities yourself with the var_dump function:
<?php
  var_dump($foo['a']); //NULL, $foo does not exist
  var_dump($_GET['non-existant-id']); //NULL, $_GET exists however the id does not.
?>

This is why isset() is important.
Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users