TimesCoding

Learn, Practice, and Excel with our Coding Tutorials!

How to Use the PHP chr() Function with Examples

June 19, 2024 2 Min read PHP

The chr() function in PHP is used to return a character from a specified ASCII value. ASCII (American Standard Code for Information Interchange) values range from 0 to 255, where each value corresponds to a specific character.

Example Usage

Here’s a simple example to demonstrate how to use the chr() function:

<?php
// ASCII value for 'A' is 65
$char = chr(65);
echo $char; // Outputs: A

// ASCII value for 'a' is 97
$char = chr(97);
echo $char; // Outputs: a

// ASCII value for '0' is 48
$char = chr(48);
echo $char; // Outputs: 0

// ASCII value for a newline character is 10
$char = chr(10);
echo $char; // Outputs a newline
?>

Practical Example

In a practical scenario, you might use the chr() function to generate characters based on their ASCII values, for instance, creating a string from ASCII values stored in an array:

<?php
$asciiValues = [72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100];
$string = '';

foreach ($asciiValues as $value) {
    $string .= chr($value);
}

echo $string; // Outputs: Hello World
?>

Handling Out of Range Values

If you provide a value outside the 0-255 range, PHP will interpret it modulo 256. Here’s how it works:

<?php
$char = chr(256);
echo $char; // Outputs: \0 (same as chr(0))

$char = chr(257);
echo $char; // Outputs: \1 (same as chr(1))
?>

Combining with ord() Function

The ord() function is the inverse of chr(). It returns the ASCII value of the first character of a string. These functions can be combined:

<?php
$originalChar = 'A';
$asciiValue = ord($originalChar);
$char = chr($asciiValue);

echo $char; // Outputs: A
?>

Conclusion

The chr() function is a simple yet powerful tool in PHP for converting ASCII values to their corresponding characters. It can be especially useful when dealing with low-level data processing or when you need to manipulate text based on ASCII values.

Tags: PHP PHP chr
Related Post:
CSRF vs XSS: Key Differences and How to Prevent Attacks − For web security, CSRF (Cross-Site Request Forgery) and XSS (Cross-Site Scripting) both are potent vulnerabilities that can wreak havoc if exploited, but they are fundamentally.
How to Use PHP iconv() Function for Effective Character Encoding Conversion − The iconv() function in PHP is used for character set conversion. This function converts a string from one character encoding to another. It is particularly.
PHP Date and Time Functions Explained with Examples − PHP offers a variety of functions for working with dates and times. These functions allow you to: Here are some commonly used functions and examples:.
How to Use PHP chunk_split() Function for String Manipulation − PHP chunk_split() Function The chunk_split() function in PHP is used to split a string into smaller chunks and add a specified string (usually a separator).
How to Use the PHP chop() Function to Trim Strings Effectively − The chop() function in PHP is used to remove whitespace or other predefined characters from the right end of a string. It’s an alias for.
How to Share Session Variables Between PHP Files for Secure CSRF Token Handling − In this tutorial, we will learn how to share session variables between PHP files, specifically focusing on handling a CSRF (Cross-Site Request Forgery) token securely..
PHP and MySQL Connection: MySQLi vs PDO Explained − Connecting PHP to MySQL is a common task in web development. Below are the steps to establish a connection using the MySQLi (MySQL Improved) extension.
PHP bin2hex() Function with Example − The bin2hex() function in PHP converts binary data into its hexadecimal representation. This can be particularly useful for encoding binary data in a readable format.
Secure Your PHP Strings with the addslashes() Function − The addslashes() function in PHP is used to escape certain characters in a string by adding backslashes before them. This is particularly useful when preparing.
PHP Sanitize Input with Example: Keeping Your Website Secure − In the dynamic world of web development, user input is the lifeblood of many applications. However, this very input can also pose a significant security.
Creating a Mobile Number Show-Hide Feature with HTML, CSS, and PHP − In today’s web development landscape, creating a user-friendly and secure user interface is crucial. One common requirement is the need to display sensitive information, such.
URL Exist or Not Exist Check PHP − We can easily check a given URL or Link is active or not. By using below PHP function we can easily get whether the given.