Doorknob Doorknob - 8 months ago 32
PHP Question

How to verify if $_GET exists?

So, I have some PHP code that looks a bit like this:

The ID is

echo $_GET["id"] . "!";


Now, when I pass an ID like
it works alright, but if there is no ID like just
then it outputs:

The ID is
Notice: Undefined index: id in C:\xampp\htdocs\myphp.php on line 9

I have searched for a way to fix this but I cannot find any way to check if a URL variable exists. I know there must be a way though.


You can use isset function:

if(isset($_GET['id'])) {
    // id index exists

You may also want to check what's inside, e.g. you can use is_int function to check if variable contains integer etc