How to Encode and Decode URL in PHP

stacktips avtar

Written by:

Editorial,  4 min read,  updated on September 17, 2023

This post explains how to encode and decode URL using PHP. PHP supports encoding and decoding of URL by providing some built-in functions. Encoding is required before sending URL data to query string or to a function which might dynamically work on this URL data. And then, this data will be decoded into its original form, after receiving it in target PHP page or function.

PHP Encode and Decode URL Example:

First, let us create an HTML file and save it as sample.html


Let us now, create a PHP file to encode and decode URL.


Lets see the simple example to encode and decode URL in PHP:

$url = "";
//encoding URL
$encodedUrl = urlencode($url);
echo $encodedUrl;

//decoding URL
echo urldecode($encodedUrl);

This all about encoding and decoding URL in PHP. Thank you for reading this article, and if you have any problem, have another better useful solution for this article, please write a message in the comment section.

This story was first published at SKPTRICKS. See the original article How to Encode and Decode URL Using PHP. Opinions expressed by Stacktips contributors are their own.