超越PHP PHP动态 | 经典文章 | CLASS | 相关下载 | 常见问题 | FORUM | WIKI | 在线手册
Site search:    
<chopchunk_split>
Last updated: Fri, 22 Jun 2007

chr

(PHP 3, PHP 4 , PHP 5)

chr -- 返回指定的字符

描述

string chr ( int ascii)

返回相对应于 ascii 所指定的单个字符。

例子 1. chr() 示例

<?php
$str
= "The string ends in escape: ";
$str .= chr(27); /* 在 $str 后边增加换码符 */

/* 通常这样更有用 */

$str = sprintf("The string ends in escape: %c", 27);
?>

你可以在此处找到 ASCII 码表:http://www.asciitable.com

此函数与 ord() 是互补的。参见 sprintf() 如何使用格式字符串 %c




add a note add a note User Contributed Notes
chr
ddawsonNOSPAM at execpc dot com
10-May-2000 07:59
[Editor's note:

%c is defined as: "Print the character belonging to the ascii code given"

chr() just gives a string, so you need to use %s, even if the string consists of only one character. This is consistent with other languages.
--Jeroen@php.net]


Learn from my mistake:
Do not expect this to work!

<?php
$c_question = chr(63);
$v_out = sprintf("<%cphp\n", $c_question);
//... more stuff being sprintf'd into v_out here ...
$v_out = sprintf("%s%c>\n", $v_out, $c_question);
$v_fp = fopen("foofile", "w");
if ($v_fp)
{
    fwrite($v_fp, $v_out, strlen($v_out));
    fclose($v_fp);
}
?>

When I did this, foofile contained <NUL NUL NUL NUL NUL>.
I spun my wheels quite awhile looking at fputs, fwrite to verify I was calling those functions correctly.
My mistake was using $c_question = chr(63) instead of
$c_question = 63 (correct).  Then everything worked fine.

<chopchunk_split>
 Last updated: Fri, 22 Jun 2007
view source | feedback | send page | sitemap | aboutus   
Copyright ® 2002-2003 PHPE.NET. All rights reserved
Last updated:2002-11-22