Contact Us

Using sprintf(), printf(), vsprintf(), vprintf(), fprintf() and vfprintf() in PHP Continued

We looked at the basics of the following functions in our last post:

sprintf(), printf(), vsprintf(), vprintf(), fprintf(), vfprintf()

Now we will expand on why it is useful in internationlising text because of it’s ability to swap Arguments around, usefully named Argument Swapping.

Argument Swapping

Let’s say you want to translate a piece of text from English to Spanish, from basic knowledge of languages from school we probably know that the adjective normally comes before the noun in English, however is reversed for many other languages, for example:

English: a blue car
Spanish: un coche azul (a car blue)

I hope this helps
This means if we were to use translation functions such as gettext() we would run into problems when simply translating words that have dynamic content.

This is where argument swapping comes in.

Consider the following:

$name = ‘Peter’;
$object = ‘car’;
$colour = ‘blue’;

printf( ‘Hi %s, you have won a %s %s’, $name, $colour, $object);
printf( ‘Hola %s, has ganado un %s %s’, $name, $colour, $object);

Now this would print perfectly in English as “Hi Peter, you have won a blue car”, but if you were translating the words to Spanish the car and colour would be the wrong way around, in comes Argument Swapping to the rescue!

If you simply place a number followed by the $ (dollar symbol) inbetween the % symbol and the format (“s” in our instance) then it tells the printf() family of functions to move around the order in which the arguments are supplied.

The example below will output the object and the colour in the correct order for both languages:

$name     = ‘Peter’;
$object = ‘car’;
$colour = ‘blue’;

printf( ‘Hi %1$s, you have won a %2$s %3$s<br />’, $name, $colour, $object );
printf( ‘Hola %1$s, has ganado un %3$s %2$s<br />’, $name, $colour, $object );

Notice the simple swapping of the 2nd and 3rd variables using the syntax “%3$s %2$s”, which tells printf() to take the third variable before the second.

Now you can simply give your translations to people to do letting them know to simply move the number around where instances like this occur.

Final Word on fprintf(), vfprintf()

We mentioned the fprintf() and vfprintf() functions and the only difference we have here is that they write the output to a file.

The syntax for these are as follows:

fprintf( stream, format, arg1, arg2, arg++ );
vfprintf( stream, format, argArray );

The only difference between printf() and vprintf() is that you enter a file open stream as the first argument to the function to write it to a file.

The following example will write our output from our example in our first post to a file called printf.txt in the same directory as our file:

$name     = ‘Peter’;
$number = 1000000;
$file     = fopen(“printf.txt”,”w”);

fprintf( $file, ‘Hi %s, you have £%u’, $name, $number );
vfprintf ( $file, ‘Hi %s, you have £%u’, array( $name, $number ) );

I hope this helps clear up any confusion with these range of functions, go ahead and enjoy playing with them.

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