86

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.

1
  • 2
    it's called isset(). if (isset($_GET['...'])) { $slide = $_GET['...']; if ($slide == 'link1): ?> content <?php endif; ?> Commented Aug 16, 2013 at 10:29

6 Answers 6

139

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.

Sign up to request clarification or add additional context in comments.

1 Comment

Older versions of PHP would let a simple if($_GET['id']!='') suffice. Stricter versions require this more explicit check I learned the hard way. While slightly annoying, I can appreciate that explicit and intentional coding builds great habits everywhere else and is a better practice, fundamentally. (can you tell I learned PHP on the web since the early 2000s?)
71
if(isset($_GET['id']))
{
    // Do something
}

You want something like that

Comments

10

Here is the PHP code to check if 'id' parameter exists in the URL or not:

if(isset($_GET['id']))
{
   $slide = $_GET['id'] // Getting parameter value inside PHP variable
}

I hope it will help you.

1 Comment

This was pretty well covered years before this answer was posted.
4

It is not quite clear what function you are talking about and if you need 2 separate branches or one. Assuming one:

Change your first line to

$slide = '';
if (isset($_GET["id"]))
{
    $slide = $_GET["id"];
}

6 Comments

Why did you put $slide = ''; at the beginning of the code? is it not superfluous
Nope it is not superfluous
So whats the purpose of it?
You should always define every variable which you are going to use.
Doesn't the 2th line make it conditional?
|
4

I know this is an old question, but since php7.0 you can use the null coalescing operator (another resource).

It similar to the ternary operator, but will behave like isset on the lefthand operand instead of just using its boolean value.

$slide = $_GET["id"] ?? 'fallback';

So if $_GET["id"] is set, it returns the value. If not, it returns the fallback. I found this very helpful for $_POST, $_GET, or any passed parameters, etc

$slide = $_GET["id"] ?? '';

if (trim($slide) == 'link1') ...

Comments

0

Why not just simplify it to if($_GET['id']). It will return true or false depending on status of the parameter's existence.

1 Comment

This will return false for urls like example.com?id= and example.com?id=0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.