Simple PHP Code To Check If URL Parameter Is Exist Or Not

By Faruque Ahamed Mollick

In web development, many times it is needed to take the value of URL parameter to process. Below is given an example of URL with value in parameter:

https://domain.come/index.php?city=kolkata

In the above URL “city’ is the parameter and “kolkata” is a value of the parameter city. A parameter in URL always followed by a question mark (?) and the value is written after an equal sign “=” next to the parameter.

Simple PHP code for file uploading process

How to get HTML form data in PHP variable?

Now suppose you want to check if the parameter in URL exists or not in PHP. Yes, often in web development project it may need to check if the parameter exists or not in URL. For example, the OpenWeatherMap API provide weather details for specific location and the location like city and country provided as URL parameter.

Now in this post, I am going to show you the simple PHP code to check if the parameter in URL exist or not.

The PHP code to check if parameter exist

We have taken the above URL with a parameter to give you the example. Here is the URL again –¬†https://domain.come/index.php?city=kolkata.

Read meta description and title tag of a web page using PHP

How to create JSON formatted text in PHP

Now below is the given PHP code which will check if the parameter “city” exists:

if(isset($_GET['city']))
{
    // Do something if parameter city is exist
}

The above code is so simple which is using an if condition. $_GET[‘city’] will get the value of “city” parameter and it is checked by PHP if condition.

Now suppose, you want to get the value of “city” parameter inside a PHP variable and want to display it on the webpage. Below is the code which will do that:

if(isset($_GET['city']))
{
     $city = $_GET['city'];
     echo $city;
}

The above code will display the value of the parameter city on the web page.

Simple PHP Code To Detect Mobile Device

I think you have now understood this tutorial. It is really good if you have got a clear idea about how URL parameter works with PHP. Web developers who work with PHP often have to work with URL parameter and it’s value.

If you still have any confusion then let me know, I will try to solve your problem.

Leave a Reply

Your email address will not be published. Required fields are marked *