Character encoding in utf-8-php

Source: Internet
Author: User
{Code...} can I explain in detail the differences between the three and determine whether they can be converted to each other?
$ Str1 = "\ xe4 \ xb8 \ xad"; $ str2 = '\ xe4 \ xb8 \ xad'; $ str3 = '中 ';

Can you explain in detail the differences between the three and determine whether they can be converted to each other?

Reply content:
$ Str1 = "\ xe4 \ xb8 \ xad"; $ str2 = '\ xe4 \ xb8 \ xad'; $ str3 = '中 ';

Can you explain in detail the differences between the three and determine whether they can be converted to each other?

The first time I answered a question on segmentfault ..

PHP string variables. double quotes and single quotes have different meanings.

When double quotation marks are used, single quotation marks are escaped without escaping.
When double quotation marks are used, the $ xxxx text will be replaced by the value of the corresponding variable.

Eg.

$ Abc = '000000'; echo "$ abc"; // This will output 123 echo '$ ABC'; // This will output $ abcecho "\ n "; // This will output a linefeed echo '\ n'; // This will output \ n two characters (one slash and one n)

Back to question,
The Chinese character in the UTF-8 in hexadecimal code is 0xe4, 0xb8, 0xad
Therefore, the double quotation mark character string is escaped as "medium" x to indicate that it is a hexadecimal character, just like & xe4; in HTML.
Xe4xb8xad

If your environment encoding is under the UTF-8, str1 and str3 are equivalent, if you directly echo, it will output "in", if it is a binary layer three byte comparison, it is completely equal, strings in PHP are directly locally encoded binary storage.

If your environment code is not a UTF-8 (such as GBK), str1 is basically garbled, and str1 and str3 are no longer equivalent.

As for str2, it outputs '\ xe4 \ xb8 \ xad' (not including quotation marks. in single quotation marks, only single quotation marks must be converted \', in other cases, \ is treated as a common character.

Only the difference between the first and second, that is, the difference between single quotation marks and double quotation marks

Double quotation marks: The quotation marks are escaped.
Single quotes: the quotation marks are not escaped.

$ A = 123; echo "output: $ a"; // output: 123 echo 'output: $ a'; // output: $ a // The following example is limited to linux's php-cliecho "new line \ nsecond line";/* will wrap and output: new linesecond line */echo 'No new line \ n aaa';/* no line feed, output: no new line \ n aaa */

\xFollowed by a hexadecimal number is an escape character, the escape character is only in double quotation marks". The single quotes only contain single quotes.'And backslash\The escape is valid, and none of the others is valid.

PHP itself does not distinguish character encoding. That is to say,$str1It is a three-byte string, and the three bytes of the string are (hexadecimal encoding)E4 B8 AD. If in UTF-8 encodingMediumWord. Other codes are not required.

While$str2It is a 12-byte string, which is the character you entered.

While$str3It is a string, if you save the file in UTF-8 encoding, then it and$str1Yes. If you save it as GBK, it is two bytes.D6 D0If you save it as BIG5A4 A4.

Whether it is UTF-8, GBK or BIG5, or even many other language encoding, are followed by EUC, that is to say, for ASCII characters, their encoding is consistent, so no matter which encoding is used to save, PHP code work will not be affected. However, non-ASCII characters are quite different.

Therefore, to properly display non-ASCII characters in PHP, ensure that your storage encoding is consistent with the output encoding. If HTML is outputmetaLabel or declare the encoding in the HTTP Header. Otherwise, garbled characters will appear.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.