I would like to write a function that (amongst other things) accepts a variable number of arguments and then passes them to sprintf().
For example:
<?php function some_func($var) { // ... $s = sprintf($var, ...arguments that were passed...); // ... } some_func("blah %d blah", $number); ?>
How do I do this in PHP?
Advertisement
Answer
function some_func() { $args = func_get_args(); $s = call_user_func_array('sprintf', $args); } // or function some_func() { $args = func_get_args(); $var = array_shift($args); $s = vsprintf($var, $args); }
The $args
temporary variable is necessary, because func_get_args
cannot be used in the arguments list of a function in PHP versions prior to 5.3.