All TalkersCode Topics

Follow TalkersCode On Social Media

PHP Get Parameter From URL

Last Updated : Jul 1, 2023

PHP Get Parameter From URL

In this article we will show you the solution of PHP get parameter from url, here first we need to define parameters with hosted website link and using ‘parse_url(), parse_str()’ methods we gets parameters from url.

The parse_url will returns array of components then which is passed to parse_str() method it will parses a query string into variables.

If array contain not variable set then it overwrites existing variables of the same name.

Step By Step Guide On PHP Get Parameter From URL :-

Here we defined url with variables and url must be hosted website’s then its stored on variable ‘$url’.

We specified two variables ‘email, name’ with respective values ‘abc@test.com, dhanu’ then we passing that url varible to parse_url() method as we know it returns array of components and its result stored on variable ‘$cmp’.

Now we have to pass this to parse_str() method there it parses as query string into variable so result of variables stored on variable ‘$res’ then printed on webpage.

<?php
$url="https://testurl.com/test/1234?email=abc@test.com&name=dhanu";
$cmp=parse_url($url);
parse_str($cmp['query'],$res);
print_r($res);
?>
  1. A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
  2. The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
  3. Here we needs to specify already hosted website link with some variable and values. So we defined url ‘https://testurl.com/test/1234?email=abc@test.com&name=dhanu’ and its stored on variable ‘$url’.
  4. In this url we specified two parameters ‘email, name’ with values ‘abc@test.com, dhanu’ then we passing variable ‘$url’ to parse_url() method which is returns url into array of components and result stored on variable ‘$cmp’.
  5. Then we passing that into parse_str() method which is parses query string to variables so we gets variables from array that is stored on variable ‘$res’.
  6. Final result of variable displayed on webpage using ‘print_r()’ method which is used for displays array like values on webpage.
  7. We can also use or specify any hosted url for this concept or if you had no hosted website link then use this and we can modify defined variables, values ‘email=abc@test.com&name=dhanu’ to some other values as per your choices.
  8. Confirm one thing before execution, format cannot change ‘after specified url use ‘?’ then define any variables and before specifying values use ‘=’ symbol then use ‘&’ symbol between variables’.

Conclusion :-

In conclusion now we are able to know how to validate form using ajax, jquery in php.

When work with php we need to create and process php files at server location and then we need to start the server before execute the program.

When we executing this program on browser we needs internet connection then can see retrieved array result of variables with values ‘Array ( [email] => abc@test.com [name] => dhanu )’.

If you’re not defined parameters at hosted link then we gets error instead of gets result array.

I hope this article on PHP get parameter from url helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Anjali

Experienced Computer Programmer with a broad range of experience in technology. Strengths in application development and Object Oriented architecture design, front end programming, usability and multimedia technology. Expert in coding languages such as C, C++ Java, JavaScript, PHP and more.

Follow Anjali On Linkedin 🡪