Utf-8-character encoding in PHP

Source: Internet
Author: User
$str1 = "\xe4\xb8\xad";$str2 = '\xe4\xb8\xad';$str3 = '中';

Can you explain in detail the difference between the three, whether it is possible to convert each other

Reply content:

$str1 = "\xe4\xb8\xad";$str2 = '\xe4\xb8\xad';$str3 = '中';

Can you explain in detail the difference between the three, whether it is possible to convert each other

Answer questions on Segmentfault for the first time.

PHP string variables, double quotes and single quotes are different in meaning.

Escaping single quotation marks when using double quotes is not escaped
When using double quotation marks, the $xxxx text is replaced by the value of the corresponding variable, and the single quotation mark does not have this effect

Eg.

$abc='123';echo "$abc"; //这样会输出123echo '$abc'; //这样会输出$abcecho "\n"; //这样会输出一个换行符echo '\n'; //这样会输出\n两个字符(一个斜杠一个n)

Back to the question,
The "Zhong" character in the UTF-8 16 binary encoding is 0XE4,0XB8,0XAD
Therefore, in a double-quoted string, it is escaped to the beginning of "medium" x to indicate that this is a hexadecimal-expressed character, as in HTML &xe4; The same
Single quote string, direct output Xe4xb8xad

If your environment code is under UTF-8, str1 and STR3 equivalent, if the direct echo, will output "medium", if it is a binary plane three byte comparison, is also exactly equal, PHP string is directly encoded in the binary storage

If your environment code is non-UTF-8 (for example, GBK), str1 is basically a garbled, str1 and STR3 are no longer equivalent

As for str2, whenever it outputs ' \xe4\xb8\xad ' (without quotes, in single-quote strings, only the single quotation mark itself needs to be escaped to \ ', other cases \ are treated as ordinary characters)

Explain only the difference between the first and the second, that is, the difference between a single quote and a double quote

Double quotation marks: The inside of the quotation mark is escaped
Single quote: Quotation marks are not escaped inside

$a = 123;echo "output:$a";//output:123echo 'output:$a';//output:$a//下面的示例仅限linux的php-cliecho "new line\nsecond line";/*会换行,输出:new linesecond line*/echo 'no new line\n aaa';/*不会换行,输出:no new line\n aaa*/

\xfollowed by the hexadecimal digits of the escape character, the escape character only works in double quotes " . Only the escape of the single quotation mark itself and the backslash is valid in single quotes ' \ , and the others are invalid.

PHP itself does not differentiate between character encodings. That $str1 is, it is a three-byte string, and the three bytes of the string are (hexadecimal encoded) respectively E4 B8 AD . If in UTF-8 encoding, it is the word. Not necessarily in other encodings.

And $str2 then a 12-byte string, which is the character you typed.

And $str3 then it's a string, and if you save the file in UTF-8 code, it's the $str1 same. If you save with GBK, it is two bytes D6 D0 , if you save it with BIG5, it is A4 A4 .

Whether it is UTF-8, GBK or BIG5, and even many other language coding, is to follow the EUC, that is, for ASCII characters, their encoding is consistent, so no matter what encoding to save, PHP code work will not affect. However, there is a big difference between non-ASCII characters.

Therefore, the non-ASCII characters in PHP to display properly, it is necessary to ensure that your save code and output encoding is consistent. If the output is HTML, it is encoded through a meta tag or in the HTTP header. If not, then there will be garbled.

  • Related Article

    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.