Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing variable argument list to sprintf()

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?

like image 207
Rob Avatar asked Oct 22 '09 12:10

Rob


1 Answers

Or better yet (and a bit safer too):

function some_func(string $fmt, ... $args) {
    $s = vsprintf($fmt, $args);
}

This is PHP 7.4, not sure if it works in earlier versions.

like image 156
mojuba Avatar answered Sep 18 '22 17:09

mojuba