String Functions

PHP chr() Function

Topic: PHP String ReferencePrev|Next

Description

The chr() function generates a character from the specified ASCII value.

This function is the inverse of the ord() function which does the opposite of what this function does.

The following table summarizes the technical details of this function.

Return Value: A single-character string corresponding to the specified ASCII value.
Changelog: Since PHP 7.4.0, this function no longer silently accepts unsupported values, and casts these to 0.
Version: PHP 4+

Syntax

The basic syntax of the chr() function is given with:

chr(ascii);

The following example shows the chr() function in action.

<?php
echo chr(50);   // Decimal value
echo chr(062);  // Octal value
echo chr(0x32); // Hex value
?>

Tip: An integer can be specified in decimal, octal or hex values. Octal values are denoted by a leading 0 (e.g. 062, 075,…), while hex values are denoted by a leading 0x (e.g. 0x32, 0x3D,…).


Parameters

The chr() function accepts the following parameters.

Parameter Description
ascii Required. An ASCII value (an integer between 0 and 255).

More Examples

Here're some more examples showing how chr() function actually works:

The following example demonstrates how to use decimal values to create ASCII Characters.

<?php
$str = "2 " . chr(43) . " 3 " . chr(61) . " 5";
echo $str;
?>

The following example demonstrates how to build a UTF-8 string from individual bytes.

<?php
$str = chr(240) . chr(159) . chr(144) . chr(158);
echo $str;
?>
Bootstrap UI Design Templates Property Marvels - A Leading Real Estate Portal for Premium Properties