PHP urlencode() vs urldecode() – Difference & Examples

Introduction

In web development, URLs often contain special characters, spaces, or symbols that cannot be directly used in a web address. To make URLs safe and valid, these characters must be encoded before being sent through the URL.

PHP provides two important functions for handling this process:

  • urlencode() converts special characters into URL-safe format.
  • urldecode() converts encoded URLs back to their original form.

Understanding the difference between PHP urlencode() vs urldecode() is important when sending and receiving data through URLs.

What is urlencode() in PHP?

The urlencode() function is used to encode a string so it can be safely used inside a URL.

It converts special characters into percent-encoded format.

For example:

Character Encoded Value
Space %20 or +
& %26
= %3D

Encoding prevents the browser from misinterpreting characters in the URL.

Syntax of urlencode()


urlencode(string)

Parameters

  • string – The string that needs to be encoded.

Return Value

  • Returns the encoded version of the string.

Example of urlencode()


$text = "PHP Tutorial Guide";
$encoded = urlencode($text);
echo $encoded;

Output

PHP+Tutorial+Guide

Explanation

Here:

  • Spaces are converted into + symbols.
  • The string becomes safe for use inside a URL.

Example URL:

example.com/search.php?q=PHP+Tutorial+Guide

Example with Special Characters


$url = "name=Rahul & course=PHP";
echo urlencode($url);

Output

name%3DRahul+%26+course%3DPHP

Explanation

  1. = becomes %3D
  2. & becomes %26
  3. Spaces become +

This prevents the browser from misinterpreting these characters.

What is urldecode() in PHP?

The urldecode() function performs the reverse operation of urlencode().

It converts encoded URL strings back into their original readable format.

This function is commonly used when retrieving data from query strings or external APIs.

Syntax of urldecode()


urldecode(string)

Parameters

  • string – The encoded string.

Return Value

Returns the decoded version of the string.

Example of urldecode()


$text = "PHP+Tutorial+Guide";
$decoded = urldecode($text);
echo $decoded;

Output

PHP Tutorial Guide

Explanation

Here:

  • The + symbols are converted back to spaces.
  • The original string is restored.

Example with Encoded URL


$url = "name%3DRahul+%26+course%3DPHP";
echo urldecode($url);

Output

name=Rahul & course=PHP

Difference Between urlencode() and urldecode()

Although both functions deal with URLs, they perform opposite tasks.

Feature urlencode() urldecode()
Purpose Encodes URL strings Decodes URL strings
Function Type Encoding Decoding
Output URL-safe format Original readable string
Usage Sending data in URL Reading URL data

Example Comparing Both Functions


$data = "PHP & MySQL Tutorial";
$encoded = urlencode($data);
$decoded = urldecode($encoded);
echo "Encoded: " . $encoded;
echo "
"; echo "Decoded: " . $decoded;

Output

Encoded: PHP+%26+MySQL+Tutorial Decoded: PHP & MySQL Tutorial

Explanation

  1. urlencode() converts special characters.
  2. urldecode() restores the original string.

Real Life Example

Suppose you are building a search feature for a website.

User searches for: PHP tutorial

The browser converts it into:

search.php?q=PHP+tutorial

In PHP:


$query = $_GET['q'];
echo urldecode($query);

Output:

PHP tutorial

Here:

  • The browser encodes the URL.
  • PHP decodes the value using urldecode().

urlencode() in Query Strings

Example:


$name = "Rahul Kumar";
$url = "profile.php?name=" . urlencode($name);
echo $url;

Output:

profile.php?name=Rahul+Kumar

This ensures the URL remains valid.

urlencode() vs rawurlencode()

PHP also provides another function:

  • rawurlencode()

Difference:

Function Space Encoding
urlencode() +
rawurlencode() %20
Example: echo rawurlencode(“PHP Tutorial”); Output: PHP%20Tutorial

Conclusion

The urlencode() and urldecode() functions in PHP are essential for handling URLs safely and correctly.

The urlencode() function converts special characters into a format that can be safely transmitted in a URL.

urldecode() restores the encoded string back to its original readable form.

These functions are commonly used when working with query strings, form submissions, APIs, and dynamic URLs.

urlencode vs urldecode – Objective Questions (MCQs)

Q1. Which function converts special characters into URL-safe format?






Q2. What does urldecode() do?






Q3. Which character does urlencode() convert spaces into?






Q4. Which function is commonly used when retrieving data from a URL?






Q5. Which function should be used before sending data via query string?