Encodera

Thanks for reading!

Donate to the site — we work hard for you.

PHP reference

urlencode

Encodes a string for use in a URL.

Syntax

urlencode(string $string): string

Parameters

ParameterTypeDescription
$stringstringThe 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%A9

A search parameter in a link:

<?php
 
$query = 'php tutorial';
 
echo '/search?q=' . urlencode($query);

Output:

/search?q=php+tutorial

Notes

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

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.

Thanks for reading!

Donate to the site — we work hard for you.