The utf8_encode Function in PHP
-
Introduction to UTF-8 and the
utf8_encode()
Function in PHP -
Purpose of the
utf8_encode()
Function in PHP
This article will introduce the utf8_encode()
function and explain its purpose with code examples.
Introduction to UTF-8 and the utf8_encode()
Function in PHP
UTF-8 is a character encoding used for Unicode. It was invented because the existing ASCII could not encode all the existing characters.
ASCII is limited to storing only 256 characters, and it could only be used for English alphabets (uppercase and lowercase) and some special characters. Then, another encoding arose as the web pages dealt with many languages used worldwide and their special characters.
ASCII could not hold all of them due to its limited space. Therefore, the concept of Unicode was introduced.
With the use of code points, Unicode can store countless characters.
UTF-8 maps each code point to its binary equivalent. As the name speaks for itself, it uses 8 bits to store a character.
In PHP, the utf8_encode()
function converts the ISO-8859-1
string to its equivalent UTF-8 version. The ISO-8859-1
string is a character set that was used in HTML 4.01.
The first 128 characters represented by this set are the same as ASCII.
However, the function utf8_encode()
does not verify whether its parameter is an ISO-8859-1
string. The function returns a UTF-8 translated string.
Let’s look at an example below of using the function.
Example Code:
$iso_string = "1 2 3";
$utf8_string = utf8_encode($iso_string);
echo $utf8_string;
Output:
1 2 3
In the example above, the strings 1 2 3
are the ISO-8859-1
encodings, translated to UTF-8 strings. Here, 1
is equivalent to 1
, 2
to 2
and 3
to 3
.
Purpose of the utf8_encode()
Function in PHP
The utf8_encode()
revolves around the concept of the UTF-8 encoding. The encoding can be used to transfer Unicode from one computer to another.
The function utf8_encode()
comes into use when we need to encode some special characters in our PHP application.
For example, store a character क
, a Devanagari script, in the $char
variable. Use the utf8_encode()
function to encode and print the encoded character.
If the file is already encoded with UTF-8, there is no need to encode the special characters with the utf8_encode()
function.
Example Code:
$char = "क";
$utf8_string = utf8_encode($char);
echo $utf8_string;
Output:
à¤
Here, the UTF-8 encoded character, à¤
, acts as a universal code for the character क
that can be used on the internet. The computer converts the encoding into machine language, a sequence of bytes, for its interpretation.
Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.
LinkedIn