PHP check if url parameter exists

php, url, url-parameters

Solution

Use isset().

if (isset($_GET["id"])) {
    // do something when a parameter exists
}

You can check the existence and equality in a single condition too

if (isset($_GET["id"]) && $_GET["id"] === 'link1') {
    // exists and equal to link1
}

However, it would be more convenient to define a variable first, using `??` shorthand, and then use it in a condition

$slide = $_GET["id"] ?? '';
...
if($slide === 'link1') {
    // equal to link1
}

Here, an empty string is used as a default value in case parameter is not defined. You can change it to any other value.

Problem

I have a URL which i pass parameters into example/success.php?id=link1 I use php to grab it ``` $slide = ($_GET["id"]); ``` then an if statement to display content based on parameter ``` <?php if($slide == 'link1') { ?> //content } ?> ``` Just need to know in PHP how to say, if the url param exists grab it and do the if function, if it doesn't exist do nothing.

Original source