Current Location: Home> Latest Articles> How to Generate Different Language Characters (Unicode) Using the chr() Function?

How to Generate Different Language Characters (Unicode) Using the chr() Function?

gitbox 2025-06-16

1. chr() Function Basic Usage

chr() function accepts an integer parameter, which represents the ASCII value of a character, and the function returns the corresponding character. For example:

echo chr(65);  // Outputs 'A'

The above code outputs the letter A because 65 corresponds to the letter A in the ASCII encoding.

chr() function can only handle integer values between 0 and 255. It can output characters in the extended ASCII range (i.e., characters between 128 and 255), but it cannot process Unicode characters.

2. Why Does chr() Not Support Unicode Characters?

Unicode is a global character encoding standard that supports characters from almost all languages, including Chinese, Arabic, Japanese, etc. The encoding range of Unicode far exceeds the 0 to 255 range. chr() is only suitable for characters within the ASCII encoding range, whereas Unicode encoding usually requires more complex processing.

For example, the Unicode encoding for the Chinese character "你" is U+4F60, which exceeds the handling range of the chr() function.

3. Using mb_convert_encoding() or json_encode() to Generate Unicode Characters

For cases where Unicode characters need to be generated, we can use other PHP functions, such as mb_convert_encoding() and json_encode(), to handle them.

Using mb_convert_encoding()

mb_convert_encoding() function allows us to convert characters from one encoding to another, supporting Unicode. When using this function, we can convert a UTF-8 encoded string to the corresponding character.

// UTF-8 string
$str = "你";
<p>// Convert the UTF-8 encoded string to Unicode encoding using mb_convert_encoding<br>
$unicode_char = mb_convert_encoding($str, 'UTF-8', 'HTML-ENTITIES');<br>
echo $unicode_char;  // Outputs the Unicode character corresponding to "你"<br>

Using json_encode() to Convert to Unicode

Another method is to use json_encode() to convert the character to Unicode encoding:

$str = "你";
$unicode_char = json_encode($str);
echo $unicode_char;  // Outputs "\u4f60"

The output \u4f60 in the above code is the Unicode encoding for the character "你".

4. Manually Generating Unicode Characters

If you know the Unicode encoding for a character, you can use PHP's mb_convert_encoding() or directly use the \u escape sequence to manually create Unicode characters.

For example, to generate the Chinese character "你":

// Use Unicode encoding to directly represent the character
echo "\u4F60";  // Outputs "你"

However, it is important to note that this method might not work directly in some PHP environments. If problems arise, it is recommended to use mb_convert_encoding() or json_encode() to handle this.

5. Using iconv() for Character Encoding Conversion

iconv() function is another tool that can be used for character encoding conversion, allowing conversion between different character encodings. It can also be useful for Unicode encoded characters.

$str = "你";
$encoded_str = iconv(&#039;UTF-8&#039;, &#039;ISO-8859-1//TRANSLIT&#039;, $str);
echo $encoded_str;

6. Summary

In PHP, the chr() function is mainly used for handling ASCII encoded characters, and it is not suitable for Unicode characters. For handling characters in different languages, especially Unicode characters, it is recommended to use mb_convert_encoding(), json_encode(), or other character encoding conversion functions.

With these methods, PHP can easily generate and handle characters from different languages, supporting the development of globalized applications.