Introduction
In this article I will explain converting between ASCII characters and codes in PHP. I want to retrieve the ASCII code corresponding to a specified character. So I will use the two important functions "chr()" and "ord()" for retrieving the ASCII code. First of all I will discuss the chr() function in PHP.
Syntax
|
chr(ASCII); |
| Parameter | Description |
| ASCII | Required an ASCII value. |
This function returns the specified character.
Example
The chr() function returns the character for the specified ASCII value and this function is the complement of ord().
<?php
$str = "The string ends in escape: ";
$str .= chr(27);
//Often this is more useful
$str = sprintf("The string ends in escape: %c", 27);
echo $str;
?>
The printf() function returns a formatted string with a format string of "%c".
Output
Example
<?php
// define ASCII code
$str1 = 65;
$str2 = 66;
$str3 = 67;
$str4 = 68;
$str5 = 69;
$str6 = 60;
$str7 = 61;
$str8 = 62;
$str9 = 63;
// retrieve character
$char1 = chr($str1);
$char2 = chr($str2);
$char3 = chr($str3);
$char4 = chr($str4);
$char5 = chr($str5);
$char6 = chr($str6);
$char7 = chr($str7);
$char8 = chr($str8);



Join the conversation! Your thoughts help the community grow.