Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
IntlChar::forDigit() function in PHP
The IntlChar::forDigit() function displays the character representation of a specified digit in the specified radix (number base). It returns the ASCII code of the character that represents the digit.
Syntax
IntlChar::forDigit(digit, radix)
Parameters
digit − A number to convert to a character (0-35)
radix − The radix (base) value. Must be between 2 and 36. Default is 10.
Return Value
The function returns the ASCII code (integer) of the character representation of the specified digit in the given radix. For digits 0-9, it returns ASCII codes 48-57 ('0'-'9'). For digits 10-35, it returns ASCII codes 97-122 ('a'-'z').
Example
The following example demonstrates basic usage ?
<?php
// Digit 7 in base 10
var_dump(IntlChar::forDigit(7));
echo "<br>";
// Digit 5 in base 16 (hexadecimal)
var_dump(IntlChar::forDigit(5, 16));
echo "<br>";
// Digit 0 in base 10
var_dump(IntlChar::forDigit(0));
echo "<br>";
// Digit 15 in base 16 (represents 'f')
var_dump(IntlChar::forDigit(15, 16));
?>
int(55) int(53) int(48) int(102)
Converting ASCII to Character
To see the actual character representation, you can use chr() function ?
<?php
echo "Digit 7: " . chr(IntlChar::forDigit(7)) . "<br>";
echo "Digit 15 in hex: " . chr(IntlChar::forDigit(15, 16)) . "<br>";
echo "Digit 25 in base 36: " . chr(IntlChar::forDigit(25, 36)) . "<br>";
?>
Digit 7: 7 Digit 15 in hex: f Digit 25 in base 36: p
Conclusion
IntlChar::forDigit() is useful for converting numeric digits to their character representations in different number bases. It returns ASCII codes that can be converted to actual characters using chr().
