php's chr and ord functions implement character addition subtraction multiplication and division operations

  • 2020-05-10 17:47:42
  • OfStack

The chr function is used to convert the ASCII code into characters
The ord function is used to convert characters to ASCII codes

The ASCII code is the encoding of all the characters that a computer can display, ranging from 0 to 255, including punctuation, letters, Numbers, Chinese characters, and so on. During programming, it is common to convert the specified characters to ASCII codes for comparison.

The following are the functions provided by PHP to convert ASCII codes and characters.
1. chr() function
This function is used to convert the ASCII code value to a string. Its function declaration is as follows:
string chr (int ascii);
2. ord() function
This function is used to convert a string to an ASCII code value. Its function declaration is as follows:
int ord(string str);
Example:
The chr() function and ord() function are used to perform the conversion operation between the string and the ASCII code. The program code is as follows:
 
<?php 
$str1=chr(88); 
echo $str1; // The return value is X 
$str2=chr(ord(X)+1); // 
echo $str2; // The return value is  Y 
echo "\t"; 
$str3=ord('S'); 
echo $str3; // The return value is 83 
?> 

Results: X Y 83

Related articles: