PHP ord function

 PHP ord function can be used to return a string of ASCII code value, for example, the letters A ASCII code 65, the @ symbol mice ASCII code is 64, word symbols # ASCII code is 35, these special characters to obtain The ASCII code can be converted through PHP's built-in ord function. The corresponding function of the ord function is the chr function . These two functions are common functions that PHP uses to convert string characters and ASCII codes. The difference between the two as follows:

  • ord function: convert string to ASCII code
  • chr function : convert ASCII code to string
PHP ord function syntax rules
int word (string $ string);
The PHP ord function has only one parameter, which is the string to be converted. The final conversion result is the ASCII decimal number representation, and only the first character of $string is converted. In other words, if we put apple in The PHP ord function will only convert the ASCII code of the first lowercase English letter a in the string .

PHP ord function application example 1: Convert a string to ASCII code
<?php
echo'Convert character # into ASCII code:'.ord('#').'<br>';
echo'Convert character% into ASCII code:'.ord('%').'<br >';
echo'Convert character @ into ASCII code:'.ord('@').'<br>';
echo'Convert character A into ASCII code:'.ord('A').'<br >';
echo'Convert the character a to ASCII code:'.ord('a').'<br>';
?>
Example output
Convert character # to ASCII code: 35
Convert character% to ASCII code: 37
Convert character @ to ASCII code: 64
Convert character A to ASCII code: 65
Convert character a to ASCII code: 97
Example one is that many common string characters are converted to ASCII decimal numbers through the PHP ord function. There are many other characters that can be converted to ASCII codes.

➤The " ASCII Code Table " on this site and the ASCII Code query tables of two external websites: ASCII-Wikipedia | Ascii Table

PHP ord function application example 2: Relative conversion with chr function
<?php
echo'<meta http-equiv="Content-Type" content="text/html; charset=utf-8">'; //Web page encoding declaration
$str1 = '66';
echo'from ASCII code 66 The result of converting back to the English letter B:'.chr($str1).'<br>';
$str2 ='B';
echo'The result of converting the English letter B to ASCII code:'.ord($str2). '<br>';
echo'First use chr to convert the ASCII code into a string, and then use ord to convert back to the ASCII code result:'.ord(chr($str1));
?>
Example output
The result of converting ASCII code 66 back to English letter B: B The result of
converting English letter B to ASCII code: 66
First use chr to convert the ASCII code to a string, and then use ord to convert back to ASCII code: 66
Example 2 shows the conversion results of numeric strings and English letter strings through the chr function and the main character ord function of this article. You can notice the output process of the fourth echo. First, use the chr function to store the number 66 in the variable . Convert to English letter B, and then use the ord function to convert English letter B back to the original ASCII code 66. ➤Continue to study the chr function relative to the ord function: PHP chr function . More PHP string processing functions

Post a Comment

0 Comments