Use of encoding="UTF-8" in php or xml?

Submitted 4 years, 2 months ago
Ticket #356
Views 398
Language/Framework Other
Priority Low
Status Closed

Example:
<?xml version = "1.0" encoding = "UTF-8" standalone = "no" ?>
<info>
   <name>Pinky</name>
   <company>MFT</company>
   <phone>140-7859-123</phone>
</info>

In first line of example we have encoding="UTF-8".

What is the use of it?

Any help please....

Thanks in advance

Submitted on Feb 22, 21

Please elaborate. Some code example might help - mateigliga 4 years, 1 month ago

yes @ mateigliga I have given example - Cheethirala 4 years, 1 month ago
add a comment

1 Answer

Verified

Here you can find an explanation of what encoding does (UTF-8 especially)
Wikipedia - UTF-8 encoding
There are other types of encoding like: ISO-8859-1, or ISO-8859-2.
You can read about ISO-8859 here:
Wikipedia - ISO-8859-1

The main idea is that encoding is used to define what set of characters is supported.
If you use UTF-8 you will be able to use any character in any language (inclusive japanesse, korean, chinesse)


If you use ISO-8859-1, you won't be able to use all characters. Some languages like japanesse, korean, chiness or arabian won't be supported. So, instead of displaying that characters, you will see some strange squares or some mallformed letters.
I think even € ("euro sign") or £ ("pound sign") aren't supported by ISO-8859-1

So, even inside XML, HTML, PHP or any other language where you manipulate strings you should set the encoding, because the browser or the database can't guess what set of characters to use.
In case of browsers, thing aren't that bad, but if you insert a character that is not supported by a database, you will have big problems when you will try to get that data from the database table.
Try to insert € ("euro sign") or £ ("pound sign") into an MySQL table (or MariaDB) that doens't support UTF-8 and see what you get. Then try to get that data from the database table using PHP. I don't think it will work, no matter what you do. Why? Because, by default MySQL (or MariaDB) uses latin1 encoding.

The main idea is: always use UTF-8 as encoding, unless you are 100% sure you can use a subset like ISO-8859-1, but even then, stick to UTF-8.

OBS:
Try to check this page source and you will see at the top:

<meta charset="utf-8">
So, even in HTML the charset (or encoding) is set to UTF-8. 

Submitted 4 years, 1 month ago


Latest Blogs