How to verify if $_GET exists?
Asked Answered
C

7

78

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.

Csc answered 18/8, 2012 at 15:14 Comment(2)
Undefined index is not an error, it is just a warning. You can turn off warning by editing the .htaccess file.Rag
verifying if $_GET exists (like in the subject) is actually another question, you're trying to verify if $_GET["id"] exists...Tibold
C
161

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);
Crystacrystal answered 18/8, 2012 at 15:15 Comment(0)
S
19
   if (isset($_GET["id"])){
        //do stuff
    }
Superintendent answered 18/8, 2012 at 15:15 Comment(0)
A
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'];
}
Adenoidal answered 18/8, 2012 at 15:17 Comment(1)
Is there any benefit to using one over the other? array_key_exists seems more right, but I've almost always used isset.Unpile
G
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.

Gaiser answered 18/8, 2012 at 15:19 Comment(5)
WTF is an example...plus it is wtf not wftGaiser
Better options would be null or 0.Adenoidal
@Adenoidal wtf is the new null :P though he was also echoing and as the OP said themselves they replaced wtf with No ID! and that's really what it was designed for, ehcoing null isn't very useful, in this case wtf was more useful than null imoGaiser
@Adenoidal Also 0 could be a valid ID so that's not such a good response for it not being set either.Gaiser
wtf is an awsome value.Upper
T
6

You are use PHP isset

Example

if (isset($_GET["id"])) {
    echo $_GET["id"];
}
Tenuto answered 18/8, 2012 at 15:16 Comment(1)
yeah .. but do to the documentation for more examplesTenuto
B
5

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

if(!empty($_GET['id'])) {
    // if get id is not empty
}
Beccafico answered 18/8, 2012 at 15:16 Comment(1)
What if the id is 0? Remember empty returns false for numerous things.Gaiser
D
0

Please try it:

if(isset($_GET['id']) && !empty($_GET['id'])){
   echo $_GET["id"];
 }
Delsiedelsman answered 16/5, 2016 at 9:31 Comment(1)
empty($_GET['id']) is translated to !isset($var) || $var == false., so the isset($_GET['id']) is not required.Dehumidifier

© 2022 - 2024 — McMap. All rights reserved.