urlencode() function
This function is used to encode a string that can be used in a url. It encodes the same way posted data from the web page is encoded. It returns the encoded string.
Syntax:-
urlencode($string_value);
Example:-
<?php
$userName="John Taylor";
echo urlencode($userName);
?>
John+Taylor
Now, you can use urlencode() into URL
<?php
$userName="John Taylor";
echo '<a href="abc.php?uname='.urlencode($userName).'">URL</a>';
?>
urldecode() function
this function is used to decode the encoded string which is created by urlencode() function.
Syntax:-
urldecode($encoded_string_value);
Example:-
<?php
$userName="John Taylor";
echo $encodedUserName= urlencode($userName); // John-Taylor
echo urldecode($encodedUserName); // John Taylor
?>
Difference between urlencode and urldecode – Objective Questions (MCQs)
Q1. Which function converts special characters into URL-safe format?
Q2. What does urldecode() do?
Q3. Which character does urlencode() convert spaces into?
Q4. Which function is commonly used when retrieving data from a URL?
Q5. Which function should be used before sending data via query string?