urlencode
Encodes a string for use in a URL.
Syntax
urlencode(string $string): stringParameters
| Parameter | Type | Description |
|---|---|---|
| $string | string | The source string. |
Return value
stringThe string with special characters replaced by codes.
Examples
Spaces and special characters:
<?php
echo urlencode('tea & coffee'), "\n";
echo urlencode('café');Output:
tea+%26+coffee
caf%C3%A9A search parameter in a link:
<?php
$query = 'php tutorial';
echo '/search?q=' . urlencode($query);Output:
/search?q=php+tutorialNotes
Without encoding, an & in a value splits the parameter in two. PHP does the reverse itself — the values in $_GET are already decoded.
See also
- http_build_query — Builds a URL query string from an array.
- parse_url — Splits a URL into its parts.
In the tutorial
- HTTP requests and responses — How PHP receives a request and builds a response — the request method and address, headers, status codes, redirects and JSON responses.