PHP – urlencode() Function Example
Php 06-Sep-2021

PHP – urlencode() Function Example

PHP urlencode() Function Example. In this tutorial we would love to share with you, how to encode given_url in PHP using the PHP urlencode() function with examples.

This tutorial has the purpose to show an easy way to encode a given URL in PHP. and Also this tutorial demonstrate to you PHP urlencode() function, definition, syntax, parameters with examples.

PHP – urlencode() Function

Definition:- The PHP urlencode() is an inbuilt function in PHP. That is used to encode the given URL. It will return an encoded string on success.

Syntax:

The basic syntax of php urlencode() function is following:

urldecode( $given_url )

Parameters of urlencode() function

This function accepts only one parameter, that is $given_url.

Example urlencode function

Let’s take the first example of PHP encode URL. Here we have one url like “https://www.tutsmake.com”, and we will encode using the PHP urlencode() function.

<?php 
 
// PHP script to encode given url using urlencode() function 
$givenUrl = "https://www.codinghelptech.com";
echo urlencode("$givenUrl") . "\n"; 
 
?>

The output of the above PHP script is following:

 https%3A%2F%2Fwww.codinghelptech.com 

Example 2 of PHP encode Urls

Let’s take the second example of PHP encode URL. Here we have multiple URLs, and we will encode using the PHP urlencode() function.

<?php 
 
// PHP script to encode, given urls using urlencode function php
 
echo urlencode("https://www.codinghelptech.com") . "<br>"; 
echo urlencode("https://www.codinghelptech.com/word-character-counter-online-tool/") . "<br>"; 
echo urlencode("https://www.codinghelptech.com/submit-tech-guest-post-write-for-us/") . "<br>"; 
 
?> 

The output of the above PHP script is the following:

 https%3A%2F%2Fwww.codinghelptech.com
 https%3A%2F%2Fwww.codinghelptech.com%2Fword-character-counter-online-tool%2F
 https%3A%2F%2Fwww.codinghelptech.com%2Fsubmit-tech-guest-post-write-for-us%2F