Javatpoint Logo
Javatpoint Logo

PHP | urlencode() Function

The "urlencode()" is one of the functions which are available in PHP by default. This function is used to encode the URL. Its working is not that complex as it looks. Instead, it is pretty simple to understand because as the output (or result), it returns a string containing all non-alphanumeric characters except (-_. ). These are replaced with the percent (%) sign followed by the two Hexa digits. To understand it more clearly lets us see its syntax in detail:



As you can see in the above-given syntax, this function accepts only a single parameter, "$input" used to hold the actual URL you want to encode.

Return Value (or output):

This function returns an encoded string upon success. Therefore the encoded or result string contains all non-alphanumeric characters except "-_." which are replaced by the "%" sign and followed by two hex digits as we have discussed above. This function encodes the URL in the same way in which data posted from the www form (or form template) is encoded.

Here we are continuously talking about URL encoding, but if you do not know anything about the URL. So it would be nice if we have some discussion about the URL to know some basics.


It is an abbreviation for Uniform Resource Locator. It can be understood as the address of a particular file or document (webpage) available on the Internet. To understand this in more detail, consider the following example:


The URL of JAVATPOINT is and the address of this page is". The elements used in this URL address are given below:

1. This "http://" part of the URL is considered the URL prefix that usually specifies the protocol used to access the location. There are many other URL prefixes other than this, which are as follows:

  1. http - It is a typical webpage or website directory, available over HTTP.
  2. FTP - It stands for File Transfer Protocol, usually used to download files from the FTP server.
  3. mailto - an email address (often used to redirect browsers to an email client)
  4. file- a file located on a local storage device (though not technically a URL because it does not refer to an Internet-based location)

2. "www." stands for World Wide Web and is used to distinguish the content. This portion of the URL is usually not required and many times can be left out or ignored.

For example, typing "" would still get you to the javatpoint website.

  1. This "" portion is considered as the domain name for the website, and the last part (or end portion) of the domain is known as the Domain's suffix(or TLD). Usually, it helps in specifying the type or location of the website on the Internet. To understand it in more details, consider the following example:


The website such as "", the domain suffix is ".com" which denotes the commercial. Many other domain suffixes are also available such as ".org" which denotes the organization, "" which is for the United Kingdom and can be used as per requirements. To get yourself a domain, you can register the name through a domain registrar.

4. "PHP" and "urlen()" are the directorieswhere the web page is located on the server. Here the given web page is two directories deep. In most servers, the public_html directory is the default directory containing the HTML files.

One can also manually enter the URL by simply typing the address in the address bar(or search box) in the web browser.

For example, you might enter a website URL printed on a business card to visit the company's website. Most URLs, however, appear automatically when you click on a rel="nofollow" target="_blank"link or open a bookmark.

If the server name in the URL is not valid or that website is no more available, in that type of scenario, your browser may display a "Server not found" error. If the path in the URL is incorrect, the server may respond with a 404 error (which denotes the incorrect path).

What exactly does the urlencode () function do?

It basically performs the URL encoding in which, all the string or combinations of several character. Special characters are converted in a specific format on the basis of some predefined rules. So that these converted or encoded URL strings can be transmitted over the Internet using the ASCII character set.

However, URLs often contain characters outside the ASCII set, so the URL must be converted into a valid ASCII format. In general, the URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits, as we have discussed earlier. In addition, a URL cannot contain spaces. Therefore the urlencode() function usually replaces a space with a plus (+) sign or with %20. To understand it, let us see some examples:

Program 1:




Some important point related to the urlencode() function

  • It would be best if you were careful about variables that may match HTML entities. The browser parses things like &amp, &copy, and &pound, and the actual entity is used instead of the desired variable name.
  • The urlencode() function is very handy while encoding a string to be used in a query part of a URL as it provides an easier way to pass the essential variable to the next page.
  • In other languages such as JavaScript and ASP, functions to encode the URL are also available. However, functions like urlencode () provided in other different languages also works on the same basic principle.

For example -

PHP has urlencode(), to encode the URL in JavaScript you can use the encodeURIcomponent() function and Server.URLEncode() function in the ASP.

Note: The JavaScript function encodes space as %20.

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA