77

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

<body>
    The ID is 

    <?php
    echo $_GET["id"] . "!";
    ?>

</body>

Now, when I pass an ID like http://localhost/myphp.php?id=26 it works alright, but if there is no ID like just http://localhost/myphp.php 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.

tckmn
  • 57,719
  • 27
  • 114
  • 156
  • 1
    Undefined index is not an error, it is just a warning. You can turn off warning by editing the `.htaccess` file. – www139 Dec 28 '15 at 22:52
  • 1
    verifying if `$_GET` exists (like in the subject) is actually another question, you're trying to verify if `$_GET["id"]` exists... – eis May 16 '16 at 09:53

7 Answers7

160

You can use isset function:

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

You can create a handy function to return default value if index doesn't exist:

function Get($index, $defaultValue) {
    return isset($_GET[$index]) ? $_GET[$index] : $defaultValue;
}

// prints "invalid id" if $_GET['id'] is not set
echo Get('id', 'invalid id');

You can also try to validate it at the same time:

function GetInt($index, $defaultValue) {
    return isset($_GET[$index]) && ctype_digit($_GET[$index])
            ? (int)$_GET[$index] 
            : $defaultValue;
}

// prints 0 if $_GET['id'] is not set or is not numeric
echo GetInt('id', 0);
Don't Panic
  • 13,965
  • 5
  • 32
  • 51
Zbigniew
  • 27,184
  • 6
  • 59
  • 66
19
   if (isset($_GET["id"])){
        //do stuff
    }
Makita
  • 1,812
  • 12
  • 15
9

You can use the array_key_exists() built-in function:

if (array_key_exists('id', $_GET)) {
    echo $_GET['id'];
}

or the isset() built-in function:

if (isset($_GET['id'])) {
    echo $_GET['id'];
}
Asaph
  • 159,146
  • 25
  • 197
  • 199
  • 1
    Is there any benefit to using one over the other? array_key_exists seems more right, but I've almost always used isset. – Rikki Dec 13 '14 at 00:18
9

Normally it is quite good to do:

echo isset($_GET['id']) ? $_GET['id'] : 'wtf';

This is so when assigning the var to other variables you can do defaults all in one breath instead of constantly using if statements to just give them a default value if they are not set.

Sammaye
  • 43,242
  • 7
  • 104
  • 146
6

You are use PHP isset

Example

if (isset($_GET["id"])) {
    echo $_GET["id"];
}
Andrew Truckle
  • 17,769
  • 16
  • 66
  • 164
Baba
  • 94,024
  • 28
  • 166
  • 217
5

Use and empty() whit negation (for test if not empty)

if(!empty($_GET['id'])) {
    // if get id is not empty
}
Julien
  • 1,946
  • 3
  • 33
  • 51
0

Please try it:

if(isset($_GET['id']) && !empty($_GET['id'])){
   echo $_GET["id"];
 }
illeas
  • 300
  • 3
  • 18
  • 2
    `empty($_GET['id'])` is translated to `!isset($var) || $var == false.`, so the `isset($_GET['id'])` is not required. – lcjury Jan 23 '17 at 13:07