How to useString Conversion and ord() Function in PHP
The basic method to find the ASCII value of each digit in a number is to convert the number to a string and then use the ord() function to get the ASCII value of each character (digit) in the string.
Example: This example shows the conversion of digits into ASCII value.
<?php
// Given number
$number = 12345;
// Convert the number to a string
$numberStr = (string) $number;
// Iterate through each character of the string
for ($i = 0; $i < strlen($numberStr); $i++) {
// Print the ASCII value of each character
echo "ASCII value of {$numberStr[$i]} is: "
. ord($numberStr[$i]) . "\n";
}
?>
Output
ASCII value of 1 is: 49 ASCII value of 2 is: 50 ASCII value of 3 is: 51 ASCII value of 4 is: 52 ASCII value of 5 is: 53
PHP Program to Print ASCII Value of all Digits of a Given Number
Given a number, the task is to print ASCII value of all digits of a given number in PHP. ASCII values are often used to represent characters and digits. Sometimes, it’s necessary to find the ASCII value of each digit in a given number, especially when dealing with character encoding or data manipulation.
Table of Content
- Using String Conversion and ord() Function
- Using str_split() and ord() Functions
- Using Mathematical Operations
Contact Us