chr()函数是PHP中的内置函数,用于将ASCII值转换为字符。它接受ASCII值作为参数,并返回表示来自指定ASCII值的字符的字符串。 ASCII值可以十进制,八进制或十六进制值指定。
八进制值由前导0定义。
十六进制值由前导0x定义。
ASCII值表可从此处引用。
用法:
string chr( $asciiVal)
参数:此函数接受单个参数$asciiVal。此参数包含有效的ASCII值。 chr()函数返回我们传递给它的ASCII值的相应字符作为参数$asciiVal。
返回值:该函数返回我们传递其ASCII值的字符。
例子:
Input: ASCII=35 ASCII=043 ASCII=0x23
Output:# # #
Explanation:The decimal, octal and hex value of '#' is
35, 043 and 0x23 respectively
Input:ASCII=48
Output:0
以下示例程序旨在说明PHP中的chr()函数:
程序1:传递不同的ASCII但等效字符相同时,演示chr()功能的程序。
// PHP program to demonstrate the chr() function
$n1 = 35;
$n2 = 043;
$n3 = 0x23;
echo "The equivalent character for ASCII 35 in decimal is ";
echo chr($n1), "\n";// Decimal value
echo "The equivalent character for ASCII 043 in octal is ";
echo chr($n2), "\n"; // Octal value
echo "The equivalent character for ASCII 0x23 in hex is ";
echo chr($n3); // Hex value
?>
输出:
The equivalent character for ASCII 35 in decimal is #
The equivalent character for ASCII 043 in octal is #
The equivalent character for ASCII 0x23 in hex is #
程序2:程序演示使用数组的chr()功能。
// PHP program to demonstrate the chr() function
// in array
$a=[48, 49, 50];
foreach($a as $i)
{
echo "The character equivalent of
ASCII value of ", $i, " is ";
echo chr($i), "\n";
}
?>
输出:
The character equivalent of ASCII value of 48 is 0
The character equivalent of ASCII value of 49 is 1
The character equivalent of ASCII value of 50 is 2