PHP chr() Function

What is PHP chr() Function?

If you want to get a character from its code point (a number), use PHP chr() function. PHP standard functions like chr() treats single byte as a character. In a single byte encoding like ASCII, ISO-8859, or Windows 1252 one byte is used to represent single character. So, using code point of a single byte encoding like ASCII, the function can determine associated character.

Relationship among character, code point, and character encoding

You know computer uses binary digits (0 and 1) to represent all digital data. But, you can’t read text if it is represented with 0 and 1. For example, “01010000 01001000 01010000” is binary representation of “PHP”.  So, how is 01010000 defined as character “P”? Well, this is where character encoding comes in. Character encoding tells computer how to interpret digital data into human readable format.

With 2 bits (0 and 1), a computer can create maximum 256 different patterns (2 to the power of 8) in a byte. With each pattern one of the 0 to 255 (total 256) is associated and it denotes a letter, number or symbol. These number are code point or codepoint or code position.

Syntax:

chr(codepoint)

Parameters:

The Function has 1 parameter which is required-

codepoint: It is an integer that ranges from 0 to 255. Check example 1 and example 2.

If you mention a code number which is outside this range, the number will be bitwise AND’ed with 255 to get the required character. For example, to get the character of code point 321-

          321 = 101000001 (binary)

            65 = 001000001 (binary)

————————————————–

321 & 65 = 001000001 = 65 (binary)

Or, you can use the following algorithm to get the required character-

while ($bytevalue < 0) {
    $bytevalue += 256;
}
$bytevalue %= 256;

As an example, for the integer -221, it is = -221+256 = 35 à 35%256 = 35. Check example 3.

Return Values:

A single character.

Examples:

Example 1: Simple chr() example-

<?php
echo "The character for the code point 65 is: " . chr(65) . "<br />";
echo "The character for the code point 97 is: " . chr(97) . "<br />";
echo "The character for the code point 49 is: " . chr(49) . "<br />";
echo chr(80).chr(72).chr(80).chr(32).chr(99).chr(104).chr(114).chr(40).chr(41);
?>

Output:

The character for the code point 65 is: A
The character for the code point 97 is: a
The character for the code point 49 is: 1

Explanation:

65, 87, and 49 are the code points of “A”, “a”, and “1” respectively according to the ASCII encoding. chr() function converts these numbers to their corresponding characters.

Example 2: More chr() examples-

<?php
for ($i=65; $i<=90;$i++){
    echo "The character for the code point $i is: " . chr($i) . "<br />";
}
?>

Output:

A B C D E F G H I J K L M N O P Q R S T U V W X Y Z

Explanation:

In ASCII (a single byte encoding), code points of upper case English alphabet characters are from 65 to 90. The loop prints convert these numbers into the Uppercase alphabet letters.

Example 3: Overflow behavior of chr() function-

<?php
echo "The character for the code point 298 is: " . chr(298) . "<br />";
echo "The character for the code point -221 is: " . chr(-221);
?>

Output:

The character for the code point 298 is: *
The character for the code point -221 is: #

Explanation:

Using the algorithm mentioned above, the code point 298 is converted to 42 (238%256=42). The function chr() convert this 42 to ASCII character *. Similarly, the code point -211 is converted to 35 (256-221 = 35, 35%256 = 35). The function chr() convert this 35 to ASCII character #.

Example 4: Random password generation with chr() function-

<?php
$password = "";
for($i=0;$i<3;$i++){
    $password .= chr(rand(65,90)).chr(rand(97,122)).chr(rand(48,57)).chr(rand(33,47));
}
echo "Random Password: " . $password;
?>

Output:

Random Password: By7&Uh2’Xo5)

Explanation:

Line 4: The rand() function generates a random integer between its first and second parameters and the chr() function converts this random integer to a character. The code points between 65 to 90 represent all the uppercase English letters. Similarly, code points 97 to 122 represents lowercase English letters, code points 48 to 57 represents digits, and code points 33 to 122 represents some special characters. With all these each password.

Note, as it is a random generated password, your password will be a different one when you run the code.

Example 5: Binary/Octal/Hexadecimal to ASCII with chr() function-

<?php
echo "The character for the code point Binary 0b1111000 is: " . chr(0b1111000) . "<br />";
echo "The character for the code point Octal 0171 is: " . chr(0171) . "<br />";
echo "The character for the code point Hexadecimal 0x7A is: " . chr(0x7A);
?>

Output:

The character for the code point Binary 0b1111000 is: x
The character for the code point Octal 0171 is: y
The character for the code point Hexadecimal 0x7A is: z

Explanation:

The binary 0b1111000 is decimal 120 and the function chr() can convert this binary value to ASCII x. The octal 0171 is decimal 121 and the function chr() can convert this octal value to ASCII y. The hexadecimal 0x7A is decimal 122 and the function chr() can convert this hexadecimal value to ASCII z.

Practical Usages of chr() Function:

You can use chr() function to generate random password. Check example 4.

Notes on chr() Function:

  1. You can also specify the integer in binary, octal or hexadecimal values.
    • A binary value starts with 0b or 0B e.g. 0b1111000. You can convert this value to ASCII character. Check example 5.
    • An octal value starts with 0o or 0O or 0 e.g. 0171. You can convert an octal value to ASCII character. Check example 5.
    • A hexadecimal value starts with 0x or 0X e.g. 0x7A. You can convert a hexadecimal value to ASCII character. Check example 5.
  2. The opposite of the chr() function is ord() function.
  3. As this function can’t use an encoding system, it can’t convert a code point of a multi-byte encoding like UTF-8, UTF-16, or UTF-32 to a character.

PHP Version Support:

PHP 4, PHP 5, PHP 7, PHP 8

Summary: PHP chr() Function

When you work with ASCII codes, understanding chr() function which is a built-in string functions is very important to work effectively.

Reference:

https://www.php.net/manual/en/function.chr.php