How to Encode and Decode url's Query Parameter in PHP

How to Encode and Decode url's Query Parameter in PHP

In this blog, you will learn how to encode and decode the query string or URL of a page.  In PHP we use query string to edit, delete data, or select data from a database by matching a particular column name.

So Professional developers use encode and decode functions to encode URL strings for special characters. There are some functions for encoding and decoding URLs as follows:

  • urlencode
  • base64_encode
  • urldecode
  • base64_decode


How to encode URL's query parameter

To encode url’s query parameter you can use both the methods together urlencode() and base64_encode().


Example:

<?php
 
$id=15;
 
$finalid=urlencode( base64_encode($id));
 
?>
 


Why encode URL's query parameter

Because URLs are limited to a number of characters like a digit, alphabets, and a few special characters like (-,_), etc, to use any other character we need encoding URL's query parameters.


How to decode URL's query parameter

To decode url’s query parameters we use urldecode() and base64_decode() function together.


Example:

<?php
 
$id="encoded string";
$finalid=base64_decode( urldecode( $id));
 
?>