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 
// 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 
// 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
相關用法
- PHP IntlChar::chr()用法及代碼示例
 - PHP imagecreatetruecolor()用法及代碼示例
 - PHP fpassthru( )用法及代碼示例
 - PHP ImagickDraw getTextAlignment()用法及代碼示例
 - PHP Ds\Sequence last()用法及代碼示例
 - PHP Imagick floodFillPaintImage()用法及代碼示例
 - PHP array_udiff_uassoc()用法及代碼示例
 - PHP geoip_continent_code_by_name()用法及代碼示例
 - PHP GmagickPixel setcolor()用法及代碼示例
 - PHP opendir()用法及代碼示例
 - PHP cal_to_jd()用法及代碼示例
 - PHP stream_get_transports()用法及代碼示例
 - PHP Ds\Deque pop()用法及代碼示例
 - PHP SimpleXMLElement children()用法及代碼示例
 
注:本文由純淨天空篩選整理自Striver大神的英文原創作品 PHP | chr() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。
