sprintf

sprintf() is a PHP native function, which format a string based on a format string, and one or more values.

sprintf() returns the formatted string, while printf() displays it immediately, and vprintf() writes the formatted string to a stream. There is also sscanf(), which parse the string, according to the format.

printf() is convenient to display text in command line, as it makes easy to use fillers and justification.

printf() also supports named arguments, which makes the format string more flexible.

<?php

// displays 123 with 9 characters, and fills with dots any empty characters
// ......123
echo sprintf("%'.9d\n", 123);

// same as above, without the echo command
printf("%'.9d\n", 123);

// same as above, without the echo command
vprintf("%'.9d\n", '......123');

?>

Documentation

See also PHP Sprintf: Powerful String Formatting

Related : String, Format