In javascript-php, why can the chr () function output unicode characters?

Source: Internet
Author: User
In php, chr () is converted between ascii characters and corresponding numbers, but why can the following code output Chinese characters? For example, Chinese characters & #039; sense & #039; unt-8 code is e6849f; $ achr (hexdec (& #039; e6 & #039 ;)). chr (hexdec (& #039; 84 & #039 ;)). chr (hexdec (& #039; 9f & #039;); echo $ a; can output Chinese characters... in php, chr () is converted between ascii characters and corresponding numbers.
But why can the following code output Chinese characters?
For example, the Chinese character 'sense' unt-8 code is e6849f;

$ A = chr (hexdec ('e6 '). chr (hexdec ('84'). chr (hexdec ('9f '));
Echo $;
Can output Chinese characters. Why?
Will the chr Union continue to merge values that exceed 127?

Reply content:

In php, chr () is converted between ascii characters and corresponding numbers.
But why can the following code output Chinese characters?
For example, the Chinese character 'sense' unt-8 code is e6849f;

$ A = chr (hexdec ('e6 '). chr (hexdec ('84'). chr (hexdec ('9f '));
Echo $;
Can output Chinese characters. Why?
Will the chr Union continue to merge values that exceed 127?

The ASCII Code represents a single-byte character (including English letters, numbers, punctuation marks, invisible characters, and control characters). It is always less than 0x80, that is, less than 128 of the decimal. When processing a character, if the byte is smaller than 0x80, it is treated as a single byte. Otherwise, it will continue to read the next byte, which is usually related to encoding, GBK processes two bytes as one character, and UTF8 requires three bytes. Sometimes PHP requires similar processing. For example, to calculate the number of characters in a string (a string may contain single-byte and multi-byte characters), The strlen method can only calculate the number of bytes, while the mb_strlen method needs to enable expansion. Similar requirements are easy to handle:

function mbstrlen($str){    $len = strlen($str);         if ($len <= 0)    {        return 0;    }         $count  = 0;         for ($i = 0; $i < $len; $i++)    {        $count++;        if (ord($str{$i}) >= 0x80)        {            $i += 2;        }    }         return $count;}

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.