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;}