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 ‘email@example.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://firstname.lastname@example.org&name=dhanu"; $cmp=parse_url($url); parse_str($cmp['query'],$res); print_r($res); ?>
- A php script can be placed anywhere in the document. A php script starts with <?php and end with ?>.
- The default file extension for php files is “.php” and php statements end with ‘;’ semicolon.
- Here we needs to specify already hosted website link with some variable and values. So we defined url ‘https://email@example.com&name=dhanu’ and its stored on variable ‘$url’.
- In this url we specified two parameters ‘email, name’ with values ‘firstname.lastname@example.org, dhanu’ then we passing variable ‘$url’ to parse_url() method which is returns url into array of components and result stored on variable ‘$cmp’.
- 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’.
- Final result of variable displayed on webpage using ‘print_r()’ method which is used for displays array like values on webpage.
- 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@example.com&name=dhanu’ to some other values as per your choices.
- 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’.
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] => firstname.lastname@example.org [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.