note 5665 modified in function.chr by vrana

From: Date: Tue, 17 Aug 2004 14:54:56 +0000
Subject: note 5665 modified in function.chr by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-80056@lists.php.net to get a copy of this message
[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. --was-- [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! $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. http://php.net/manual/en/function.chr.php

« previous php.notes (#80056) next »