Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. urlencode() / urldecode() / base64_encode()

urlencode() / urldecode() / base64_encode() Since: PHP 4(2000)

Encodes and decodes strings for use in URLs, and converts binary data to Base64 format. Use these functions for safely passing URL parameters and encoding data.

Syntax

// URL-encodes a string. Spaces are converted to '+'.
urlencode($string);

// URL-decodes a string.
urldecode($string);

// URL-encodes a string per RFC 3986. Spaces are converted to '%20'.
rawurlencode($string);

// URL-decodes a string per RFC 3986.
rawurldecode($string);

// Base64-encodes a string.
base64_encode($string);

// Base64-decodes a string.
base64_decode($string, $strict);

Functions

FunctionDescription
urlencode($string)URL-encodes a string. Spaces are converted to '+' and other special characters to '%XX' format. Use this for query parameter values.
urldecode($string)Decodes a URL-encoded string back to its original form.
rawurlencode($string)Encodes a string per RFC 3986. Spaces are converted to '%20'. Use this for the path portion of a URL.
rawurldecode($string)Decodes a string per RFC 3986.
base64_encode($string)Encodes a string to Base64 format, allowing binary data to be safely handled as text.
base64_decode($string, $strict)Decodes a Base64-encoded string. If $strict is set to true, returns false when the input contains invalid characters.

Return Value

Returns the encoded or decoded string. base64_decode() returns false if decoding fails.

Sample Code

<?php
// URL-encode a Japanese string.
echo urlencode("東京都"); // Outputs '%E6%9D%B1%E4%BA%AC%E9%83%BD'.

// Example of building a query parameter with URL encoding.
$keyword = "PHP tutorial";
$url = "https://example.com/search?q=" . urlencode($keyword);
echo $url; // Outputs 'https://example.com/search?q=PHP+tutorial'.

// rawurlencode() converts spaces to '%20'.
echo rawurlencode("PHP tutorial"); // Outputs the string with spaces as '%20'.

// Decode a URL-encoded string back to the original.
echo urldecode("%E6%9D%B1%E4%BA%AC%E9%83%BD"); // Outputs '東京都'.

// The difference between urlencode() and rawurlencode().
echo urlencode("hello world"); // Outputs 'hello+world'.
echo rawurlencode("hello world"); // Outputs 'hello%20world'.

// Base64 encoding.
echo base64_encode("Hello PHP"); // Outputs 'SGVsbG8gUEhQ'.

// Decode Base64 back to the original string.
echo base64_decode("SGVsbG8gUEhQ"); // Outputs 'Hello PHP'.

// Example of encoding data for an email attachment or API request.
$data = json_encode(["user" => "John", "action" => "purchase"]);
$encoded = base64_encode($data);
echo $encoded; // Outputs the data as a safe Base64 string.

// Decode back to the original data.
$decoded = json_decode(base64_decode($encoded), true);
echo $decoded["user"]; // Outputs 'John'.

// Example of safely building multiple query parameters at once.
$params = [
	"name" => "John Smith",
	"city" => "New York"
];
echo http_build_query($params); // Automatically encodes all parameters.

Notes

urlencode() encodes values for use in URL query parameters, allowing special characters such as Japanese text and spaces to be safely included in a URL. urlencode() converts spaces to '+', while rawurlencode() converts them to '%20'. In general, use urlencode() for query parameter values and rawurlencode() for the path portion of a URL.

When you need to encode multiple parameters at once, http_build_query() is the convenient choice. Simply pass an associative array and it automatically encodes all keys and values into a query string.

base64_encode() converts binary data to an ASCII string, and is used for email attachments and sending or receiving data via APIs. Base64 is an encoding scheme, not encryption — do not use it to protect sensitive data. To escape HTML output, use htmlspecialchars().

If you find any errors or copyright issues, please .