The functions of these two functions are the opposite. The chr function returns characters from the specified ASCII value, while the ord () function returns the ASCII value of the first character of the string. You can use this letter to understand this.
The functions of these two functions are the opposite. The chr function returns characters from the specified ASCII value, while the ord () function returns the ASCII value of the first character of the string. You can use this function to understand this.
First look at the chr function
The chr () function returns characters from the specified ASCII value.
Chr (ascii)
Ascii parameters can be decimal, octal, or hexadecimal. You can specify the octal ratio by using the frontend 0 and the hexadecimal value by using the frontend 0x.
Example,The code is as follows:
- Echo chr (52 );
- Echo chr (052 );
- Echo chr (0x52 );
- ?>
- // Output:
- 4
- *
- R
Isn't it amazing? in fact, it's not surprising that I often use chr to operate some codes that do not see the encoding. the code is as follows:
- Echo chr (13 );
- Echo chr (32 );
- ?>
Let's think about what this will output. The result is a carriage return and a space. let's look at the ord function. the ord () function returns the ASCII value of the first character of the string, from the above, it is exactly the opposite of chr.
Syntax:Ord (string)
Example
- Echo ord ("h ");
- Echo ord ("hello ");
- ?>
Output result:104 104
Now let's look at a comprehensive example. the code is as follows:
- $ 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; // return value: 83
- ?>