Contact Us

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.

Usage

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.

 

 

This article was posted by



Leave a Reply

What our clients say

"It’s like having your own in-house digital marketing consultancy rather than an external company. I no longer think about them as a supplier. They’re part of the team."
Marketing Manager, Education
"They have always been professional and focused on delivering results in a timely and cost effective manner. It is enlightening to have an organisation that takes the time to make recommendations on actual data analysis."
Business Manager, Charity
"They (Network Intellect) continue to exhibit their digital expertise by helping us to establish a competitive advantage by pushing the boundaries and innovating their technology and performance solutions."
Head of Marketing Communications, Education
"Because of their talent, expertise and level of service our on-line business has increased dramatically."
Founder, Electronics
"I would have no hesitation in recommending them as an analytics partner to understand and improve the performance of a website."
Vice President, Retail
Network Intellect continue to give us a fantastic service, fast and friendly and are always ready to go the extra mile. They have gone above and beyond the call of duty to deliver our results.
Director, E-Cigarette Web
"We found that Network Intellect had the depth of knowledge we needed and after speaking to them a few times we decided to go with them. It’s the best decision we’ve made in some time."
Owner, Retail
"Their services offered great value for money. I would not hesitate to recommend them as a digital partner to others."
Strategic Marketing Manager, Education
"Jason and his team at Network Intellect embody efficiency and efficacy. I would have no hesitation in recommending this excellent company to others."
Marketing Manager, Education
Network Intellect have managed our product data feeds with exceptional care and meticulous detail. I know I can trust them to make the right decisions for our business."
Director, E-Commerce
"It’s like having your own in-house digital marketing consultancy rather than an external company. I no longer think about them as a supplier. They’re part of the team."
Marketing Manager, Education
Read all testimonialsRead all testimonials

We can help you be great

We have friendly staff ready to help you get on your path to greatness...
Network Intellect Menu