Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP check if url parameter exists

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.

Thanks Guys

like image 857
user2389087 Avatar asked Aug 16 '13 10:08

user2389087


People also ask

How to check parameter in url in php?

Use parse_url() and parse_str() Functions to Get Parameters From a URL String in PHP. We can use the built-in functions parse_url() and parse_str() functions to get parameters from a URL string .

How does PHP Isset work?

PHP isset() Function The isset() function checks whether a variable is set, which means that it has to be declared and is not NULL. This function returns true if the variable exists and is not NULL, otherwise it returns false.

What function is used to determine if a value was sent via query string?

In general you can check the $_GET array to see how many variables have been sent and act accordingly, by using count($_GET) .


1 Answers

Use isset()

$matchFound = (isset($_GET["id"]) && trim($_GET["id"]) == 'link1'); $slide = $matchFound ? trim($_GET["id"]) : ''; 

EDIT: This is added for the completeness sake. $_GET in php is a reserved variable that is an associative array. Hence, you could also make use of 'array_key_exists(mixed $key, array $array)'. It will return a boolean that the key is found or not. So, the following also will be okay.

$matchFound = (array_key_exists("id", $_GET)) && trim($_GET["id"]) == 'link1'); $slide = $matchFound ? trim($_GET["id"]) : ''; 
like image 183
8 revs, 2 users 80% Avatar answered Oct 23 '22 11:10

8 revs, 2 users 80%