Using sprintf(), printf(), vsprintf(), vprintf(), fprintf() and vfprintf() in PHP
One of the first functions set a PHP developer is usually presented with in his textbook or online course is the following collection:
sprintf(), printf(), vsprintf(), vprintf(), fprintf(), vfprintf()
I admit that you may not have seen the ones with the v or f prefixes but you have probably at least seen the first two sprintf() and printf() along your way.
It can be confusing to some where printf() or sprintf() are actually useful and it may not be until you get in to more advanced projects for example when dealing with site translations etc. that you really see the full power of these functions but you can still get into using them now in your programmes.
For the start let’s just point out that printf() and sprintf() are basically the same function, and sprintf() can be remembered using the phrase “silent printf()”, because the only difference is that sprintf() doesn’t output anything to the browser so can be saved in a variable.
The following 2 code lines are basically identical:
$name = “Peter”;
printf( “Hello %”, $name );
$name = “Peter”;
print sprintf( “Hello %”, $name );
printf(), sprintf(), vsprintf() and vprintf() Syntax
As sprintf() and printf() are the same I will just use printf() in the examples but all will work by putting an s at the beginning. First of all the syntax is pretty straightforward and is as follows:
printf( format, arg1, arg2, arg++ );
Again the only difference with the vprinf() and vsprintf() functions is that the second argument can be passed in as an array, as below:
vprintf( format, argArray );
So our collection of 6 scary functions above are really in affect just 3 with slightly different formatting and we will see that the vfprintf() and fprintf() functions are just as easy really and build on the functions we have already learnt.
The printf() collection of functions lets you insert specific variables into your strings and deals with certain formatting of these values (helpful when requiring numerical values for examples) as well as allowing argument swapping (very useful for language translation).
To use placeholders in this collection of functions you need to use a % symbol followed by a format that the input should be recieved in, you can find the full list in the PHP manual here.
For our example though we will keep it basic, using “s” for string and “u” for an integer. Below is a basic example using a numerical value and a string:
$name = ‘Peter’;
$number = 1000000;
printf( ‘Hi %s, you have £%u’, $name, $number );
print sprintf (‘Hi %s, you have £%u’, $name, $number );
vprintf (‘Hi %s, you have £%u’, array( $name, $number ) );
print vsprintf (‘Hi %s, you have £%u’, array( $name, $number ) );
This will output “Hi Peter, you have £1000000” 4 times, not bad hey ;)
In the next chapter we will look at Argument Swapping and the final 2 functions fprintf(), vfprintf().
If you would like any help with any area of web development then why not get in contact with us here at Network Intellect.