chr

(PHP 4, PHP 5, PHP 7, PHP 8)

chrGenerate a single-byte string from a number

Description

chr(int $codepoint): string

Returns a one-character string containing the character specified by interpreting codepoint as an unsigned integer.

This can be used to create a one-character string in a single-byte encoding such as ASCII, ISO-8859, or Windows 1252, by passing the position of a desired character in the encoding's mapping table. However, note that this function is not aware of any string encoding, and in particular cannot be passed a Unicode code point value to generate a string in a multibyte encoding like UTF-8 or UTF-16.

This function complements ord().

Parameters

codepoint

An integer between 0 and 255.

Values outside the valid range (0..255) will be bitwise and'ed with 255, which is equivalent to the following algorithm:

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

Return Values

A single-character string containing the specified byte.

Changelog

Version Description
7.4.0 The function no longer silently accepts unsupported codepoints, and casts these to 0.

Examples

Example #1 chr() example

<?php
// Assumes the string will be used as ASCII or an ASCII-compatible encoding

$str "The string ends in escape: ";
$str .= chr(27); /* add an escape character at the end of $str */

/* Often this is more useful */

$str sprintf("The string ends in escape: %c"27);
?>

Example #2 Overflow behavior

<?php
echo chr(-159), chr(833), PHP_EOL;
?>

The above example will output:

aA

Example #3 Building a UTF-8 string from individual bytes

<?php
$str 
chr(240) . chr(159) . chr(144) . chr(152);
echo 
$str;
?>

The above example will output:


🐘

See Also

Here you can write a comment


Please enter at least 10 characters.
Loading... Please wait.
* Pflichtangabe
There are no comments available yet.

PHP cURL Tutorial: Using cURL to Make HTTP Requests

cURL is a powerful PHP extension that allows you to communicate with different servers using various protocols, including HTTP, HTTPS, FTP, and more. ...

TheMax

Autor : TheMax
Category: PHP-Tutorials

Midjourney Tutorial - Instructions for beginners

There is an informative video about Midjourney, the tool for creating digital images using artificial intelligence, entitled "Midjourney tutorial in German - instructions for beginners" ...

Mike94

Autor : Mike94
Category: KI Tutorials

Basics of views in MySQL

Views in a MySQL database offer the option of creating a virtual table based on the result of an SQL query. This virtual table can be queried like a normal table without changing the underlying data. ...

admin

Autor : admin
Category: mySQL-Tutorials

Publish a tutorial

Share your knowledge with other developers worldwide

Share your knowledge with other developers worldwide

You are a professional in your field and want to share your knowledge, then sign up now and share it with our PHP community

learn more

Publish a tutorial