URL Encoding Functions Comparison
PHP provides three main functions for URL encoding:
These functions serve different purposes and offer distinct ways to generate or process URL strings. This section explains the purpose and features of each function.
Function Purpose & Features
The following table compares the primary uses of each function.
Comparison Item | urlencode() |
rawurlencode() |
http_build_query() |
---|---|---|---|
Primary Use | Mainly used to encode strings that will be used as values in URL query parameters.
Especially convenient for encoding variable values passed to the next page as URL query values. |
Used to safely encode individual URI components (e.g., search terms, filenames, query parameter values) according to the URI standard defined in RFC 3986. | Encodes arrays or objects into a URL-encoded query string.
This function is very useful when generating HTTP requests or URLs based on arrays or objects. |
urlencode()
The following example demonstrates how to use urlencode()
to encode strings for use as URL query parameter values.
// Value to be passed to the next page
$search = 'hello world!';
// Encode the value for use in a query string
$encodedSearch = urlencode($search);
// Append the encoded value to the URL
$url = 'https://example.com/search.php?q=' . $encodedSearch;
echo $url;
// Output: 'https://example.com/search.php?q=hello+world%21'
Features
- Spaces are encoded as plus signs (
+
). - This behavior aligns with the way HTML form data is transmitted using the
application/x-www-form-urlencoded
MIME type. urlencode()
is designed to comply with HTML form submission rules, where spaces are encoded as+
.- Because of this,
urlencode()
is primarily used for encoding strings as URL query parameter values.
rawurlencode()
rawurlencode()
encodes most special characters, making it useful for safely encoding individual URI components such as search terms, filenames, or query parameter values.
$keyword = 'iPhone & Galaxy/Note=100% #1';
$encoded = rawurlencode($keyword);
echo 'https://example.com/search?query=' . $encoded;
// Output: 'https://example.com/search?query=iPhone%20%26%20Galaxy%2FNote%3D100%25%20%231'
Features
rawurlencode()
encodes characters with special meaning in URLs, such as protocol, domain, and path separators (/
,:
,?
,&
,#
), following RFC 3986.- It encodes most special characters, making it ideal for safely encoding individual URI components.
- Spaces are encoded as
%20
, unlikeurlencode()
which uses+
. - For cases where spaces need to be encoded as
+
(typically when sending HTML form data),urlencode()
is preferred overrawurlencode()
.
http_build_query()
http_build_query()
converts arrays or objects into URL-encoded query strings. It is highly useful for creating HTTP requests or URLs based on arrays or objects.
Here is a basic example of building a URL query string from an array:
$data = array(
'name' => 'John Doe',
'age' => 30,
'city' => 'New York'
);
$queryString = http_build_query($data);
echo $queryString;
// Output: 'name=John+Doe&age=30&city=New+York'
Features
- Rather than encoding individual strings, this function encodes entire arrays or objects into query string format.
- It is very useful when generating HTTP requests or URLs based on structured data like arrays or objects.
See also
- PHP urlencode() Function – Encoding a URL Query Parameter Value
- PHP rawurlencode() Function – Safely Encoding URI Components
- PHP http_build_query() Function – Generate URL-Encoded Query Strings from Arrays or Objects
- PHP urldecode() Function – Decoding URLs Encoded with urlencode()
- PHP rawurldecode() Function – Decoding URLs Encoded with rawurlencode()
- PHP parse_str() Function – Parse URL Query Strings into Variables