note 93196 deleted from function.echo by danbrown

From: Date: Thu, 27 Aug 2009 22:11:00 +0000
Subject: note 93196 deleted from function.echo by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-159955@lists.php.net to get a copy of this message
Note Submitter: Anonymous ---- This results in a VERY strange error. <?php class String { public static function Format() { $argc = func_num_args(); $argv = func_get_args(); for ($arg_n = 0; $arg_n < $argc; $arg_n++) { $argv[0] = str_replace("\{$arg_n\}", $argv[$arg_n + 1], $argv[0]); } return $argv[0]; } } echo String::Format("My name is {0} and I am {1} years old", "Thomas", 18); ?> If I'm not mistaken, I have PHP 5.2 installed, which means the bug is probably still there, IF it is a bug. If you remove the \ (backslashes) that is right before {$arg_n} (which was my original code; I had forgotten that PHP uses them to isolate the variable in the string) the codes works fine (except the braces are now around the values, but no bugs are present). If the bug is fixed and you therefore don't see it, the bugs results in this: My name is {0} and I am {1} years old This works, however: $argv[0] = str_replace('{' . $arg_n . '}', $argv[$arg_n + 1], $argv[0]); It is of course no big disaster as I have this workaround fixing it, but is is a bit curious. I have of course considered the possibility that this is all my fault, and you simply cannot disable the function of the braces with a backslash, but you can disable the $ so one would not without reason assume that \{ would disable {.

« previous php.notes (#159955) next »