note 42598 added to function.vsprintf
| From: | jed at NOSPAM dot jed dot bz | Date: | Sat, 22 May 2004 22:48:36 +0000 |
| Subject: | note 42598 added to function.vsprintf | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-69974@lists.php.net to get a copy of this message | ||
vsprintf() accepts arrays with any keys, so the array_shift() technique is unnecessary when writing
a printf-type function. Any parameters you require are easily unset from the array you retrieve with
func_get_args():
<?php
function mysprintf($format) {
$args = func_get_args();
unset($args[0]); /* get rid of "$format" */
return vsprintf($format, $args);
}
/* I use this technique in production code as follows: */
function logf($target, $string) {
$args = func_get_args();
unset($args[0], $args[1]);
fprintf($GLOBALS['config']['logtargets'][$target],
"[%s] %s\n", date('H:i'), wordwrap(vsprintf($string, $args), 75, '\n\r
'));
}
/* e.g.:
logf(DEBUG, "Oops! %s", mysql_error());
*/
?>
array_shift() and other costly array operations aren't required, as far as I know. I could be
wrong.
----
Manual Page -- http://www.php.net/manual/en/function.vsprintf.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42598
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42598&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42598&report=yes
Search -- http://master.php.net/manage/user-notes.php