Need help understanding sprintf() function

I’ve read the PHP manual and still don’t understand. Can anyone give me simple examples of how this function is used?

Yes that helps, thanks. I just don’t see any use for it. Are there any real applications where it comes in handy?

printf( 'There are %d monkeys in the %s', 10, 'trees' );
// outputs: There are 10 monkeys in the trees

Good enough?

sprintf allows you to format the fields as you output them - something you can’t do with print without adding additional processing.

I prefer sprintf for readability. I find it much easier to read placeholders than variables embedded in strings, unless there are a large amount of variables that is. Sprintf takes some time getting use to thus its not hard to see why people who are unfamiliar with it think its less readable. I also prefer it when not using bind statements as the SQL can easily be dumped and tested w/o replacing a whole bunch of placeholders before running the query directly through the MySQL command prompt.

We often use sprintf for translated text like sprintf(_('Hello, %s'), $username) where the actual text provided eventually to sprintf can basically be anything (most usually, no translation :x). Of course, the above is possible with many different approaches but it works well for us.

To each his own I suppose. Coming from a C background, I’m very used to the printf format. Still use echo most of the time but the more parameters I have the more likely I’ll use printf.

Also useful for formatting


$int = 4;
$double = 1.0 / 3.0;
sprintf("%02u %.2f\
",$int,$double);

The %02u says to write the integer using 2 characters, adding leading 0’s if needed.


   $msg = sprintf("%s %s, Total: %u, Inserted: %u, Updated: %u",
      get_class($this), 
      basename($this->innFileName),
      $count->total,$count->inserted,$count->updated);

Handy when you need to call functions but don’t really need to store the results. And can be easier to read.

that is harder to read imho. You need to do a mental match up of which parameter is going where. The more parameters the worse it gets.

Well next to one another like that they’re functionally identical, yes. But if you want to define the string outside the scope of $s and use it after $s has been defined or use it multiple times with different parameters there’s quite a big difference.

On the whole though, it is a rather useless function.


sprintf ("There are %s monkeys in the barrel");

// is no better in any way than

print ("There are $s monkeys in the barrel");


It’s just one of PHP’s quirks, like how all the array functions are needle, haystack in sequence and most of the string functions are haystack, needle arranged… And underscores are used sometimes (str_replace) and not at others (stristr) and camel case turns up in the newer libraries.

Language files are a good use of it. e.g. “You have %s unread messages” may literally translate as “%s of your messages are unread” in another language.

Using sprintf for the sake of it is bad.