How to verify if $_GET exists?

0 votes

 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 cannot find any way to check if a URL variable exists. How to do that?

Oct 30 in PHP by kartik
• 35,500 points
44 views

1 answer to this question.

0 votes

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);
answered Oct 30 by Niroj
• 76,940 points

Related Questions In PHP

0 votes
1 answer

How to check if a file exists from a url using php?

Hello, You have to use CURL function does_url_exists($url) { ...READ MORE

answered Nov 3 in PHP by Niroj
• 76,940 points
68 views
0 votes
1 answer

How can one check to see if a remote file exists using PHP?

Hello @kartik, You can instruct curl to use ...READ MORE

answered Nov 10 in PHP by Niroj
• 76,940 points
26 views
0 votes
1 answer

How to check if php session is already started or not?

Hello kartik, Use session_id(), it returns an empty string ...READ MORE

answered Apr 1 in PHP by Niroj
• 76,940 points
771 views
0 votes
1 answer

How to put an 'if clause' in an SQL string?

Hello @kartik, For your specific query, you can ...READ MORE

answered Jun 1 in PHP by Niroj
• 76,940 points
82 views
+1 vote
1 answer

How to make anchor tag with routing using Laravel?

Hey @kartik, First you have to go to ...READ MORE

answered Mar 18 in Laravel by Niroj
• 76,940 points
3,470 views
0 votes
1 answer

What is redirection in Laravel?

Named route is used to give specific ...READ MORE

answered Mar 18 in Laravel by Niroj
• 76,940 points
412 views
0 votes
1 answer

How to install Laravel via composer?

Hello, This is simple you just need to ...READ MORE

answered Mar 23 in Laravel by Niroj
• 76,940 points
286 views
+1 vote
1 answer

What are named routes in Laravel and How can specify route names for controller actions?

Hey @kartik, Named routing is another amazing feature of ...READ MORE

answered Mar 23 in Laravel by Niroj
• 76,940 points
4,167 views
0 votes
1 answer

How to drop a table if it exists?

Hello @kartik, No. That will drop the table only ...READ MORE

answered Jul 21 in PHP by anonymous
• 76,940 points
43 views
0 votes
1 answer

How to check if multiple array keys exists?

Hello @kartik, If you only have 2 keys ...READ MORE

answered Oct 27 in PHP by Niroj
• 76,940 points
25 views