Using the ord function to obtain the ASCII code of a character in PHP is a very basic operation, and it is also one of the very important knowledge points for developers who are new to the PHP language. This article will introduce how to use the ord function to obtain the ASCII code of a character.
1. Introduction to ord function
The ord function is a built-in function in PHP, which is used to obtain the ASCII code of a character. The syntax of this function is as follows:
int ord ( string $string )
Among them, $string is the character whose ASCII code is to be obtained, and the return value is the ASCII code value of the character (0~255).
2. Use of ord function
The following uses several examples to demonstrate how to use the ord function to obtain the ASCII code of a character.
Code example:
<?php $str = "A"; $ascii = ord($str); echo "字符 $str 的ASCII码为 $ascii"; ?>
Output result:
字符 A 的ASCII码为 65
Code description:
In the above code, we use the ord function to obtain the ASCII code of the character "A", and then output the result through the echo statement.
Code example:
<?php $str = "hello, world!"; for ($i = 0; $i < strlen($str); $i++) { $ascii = ord($str[$i]); echo "字符 $str[$i] 的ASCII码为 $ascii <br/>"; } ?>
Output result:
字符 h 的ASCII码为 104 字符 e 的ASCII码为 101 字符 l 的ASCII码为 108 字符 l 的ASCII码为 108 字符 o 的ASCII码为 111 字符 , 的ASCII码为 44 字符 的ASCII码为 32 字符 w 的ASCII码为 119 字符 o 的ASCII码为 111 字符 r 的ASCII码为 114 字符 l 的ASCII码为 108 字符 d 的ASCII码为 100 字符 ! 的ASCII码为 33
Code description :
In the above code, we traverse each character in the string through a for loop, then use the ord function to obtain the ASCII code of each character, and finally output the result through the echo statement.
In some cases, the characters in the string may not be ASCII encoded. For example, when using Chinese characters, you need to use character sets such as UTF-8. In this case, the value obtained by the ord function may not be the ASCII code value we expect. At this time, other functions need to be used for character encoding conversion. Here is an example:
Code example:
<?php $str = "你好"; $utf8 = iconv("GB2312", "UTF-8", $str); for ($i = 0; $i < strlen($utf8); $i++) { $ascii = ord($utf8[$i]); echo "字符 $utf8[$i] 的ASCII码为 $ascii <br/>"; } ?>
Output result:
字符 你 的ASCII码为 228 字符 好 的ASCII码为 189 字符 好 的ASCII码为 173
Code description:
In the above example, we first use iconv The function converts the string from GB2312 encoding to UTF-8 encoding, and then uses the ord function to obtain the ASCII code of each character. Note that when performing character encoding conversion, you need to make a selection based on the actual situation.
3. Summary
Using the ord function to obtain the ASCII code of a character is a very basic operation and is very practical in PHP programming. This article demonstrates through several examples how to use the ord function to obtain the ASCII code of a character. I hope it will be helpful to beginners.
The above is the detailed content of How to use the ord function to get the ASCII code of a character in PHP. For more information, please follow other related articles on the PHP Chinese website!