void printf ( string format [, mixed args [, mixed ...]])
Printf() may well not be a function you will use too often, but many people do, so it is good for you to know. Printf() is the C and C++ way to format text, and it is been copied wholesale into PHP for those who want to make use of it. It is not easy to use, but if you are doing a lot of code formatting it will produce shorter code.
Printf() takes a variable number of parameters - a format string is always the first parameter, followed by zero or more other parameters of various types. Here is a basic example:
<?php
$foo = "lions, tigers, and bears";
printf("There were %s - oh my!", $foo);
?>
That will put together the string "There were lions, tigers, and bears - oh my!" and send it to output. %s is a special format string that means "string parameter to follow", which means that $foo will be treated as text inside the string that printf() creates.
Here is another example, slightly more complicated this time:
<?php
$foo = "you";
$bar = "the";
$baz = "string";
printf("Once %s've read and understood %s previous section, %s should be able to use %s bare minimum %s control functions to help %s make useful scripts.", $foo, $bar, $foo, $bar, $baz, $foo);
?>
This time we have several %s formatters in there, and the corresponding number of variables after parameter one. PHP replaces the first %s with parameter two, the second %s with parameter three, the third %s with parameter four, and so on. Note that we have both $foo and $bar appearing more than once in the format list, which is perfectly acceptable.
Printf() takes a variety of other format strings as well as %s. Here is the complete list:
Format |
Meaning |
%% |
A literal percent character; no matching parameter is required |
%b |
Parameter is an integer; present it as binary |
%c |
Parameter is an integer; present it as a character with that ASCII value |
%d |
Parameter is an integer; present it as a signed number |
%f |
Parameter is a float; present it as a float |
%o |
Parameter is an integer; present it as octal |
%s |
Parameter is a string; present it as a string |
%x |
Parameter is an integer; present it as hexadecimal with lowercase letters |
%X |
parameter is an integer; present it is hexadecimal with uppercase letters |
A few of those may well not make sense at first, so an example is called for. One quick note, first, though: if, for example, you use %s then pass a number in the corresponding parameter, PHP will treat the number as a string. The same goes for all parameters - if you specify one type then pass in another in its place, PHP will treat it as the type you specified it was, not as the type it actually is. This is a good thing, because you cannot always be sure what type a variable is, yet you can always be sure what kind of variable you would like it to be.
<?php
$number = 123;
printf("123 in binary is: %b", $number);
printf("123 in hex is: %h", $number);
printf("123 as a string is: %s", $number);
printf("%% allows you to print percent characters");
?>
If you are thinking that printf() might not sound useful, you perhaps have not yet grasped quite how much it can do for you. Consider, for example, that you can now put the strings for parameter one separate from the printf() call, which means you can change languages at the drop of a hat.
Furthermore, it means you do not need to add new variables to your script to perform conversions - printf() will do them all for you, particularly thanks to an extra piece of functionality it has, revolving around the use of . (a full stop). Take this piece of code as an example:
<?php
$number = 123.456;
$formatted = number_format($number, 2) . "\n";
print "Formatted number is $formatted\n";
printf("Formatted number is %.2f\n", $number);
?>
In that code lines two and three handle rounding our float to two decimal places then printing out the result. However, the same thing is accomplished in line three - %f is the format term meaning "float", but by preceding the F with .2 (that is a full stop followed by a two) we're instructing printf() to round the float to two decimal places. We could have used .1 for one decimal place, .8 for eight decimal places, etc. So, as you can see, printf() can make your code a great deal easier to read, as well as allowing you to keep your text separate from your variables!
Want to learn PHP 7?
Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today!
If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it!
Next chapter: Parsing a string into variables >>
Previous chapter: Padding out a string
Jump to:
Home: Table of Contents
Copyright ©2015 Paul Hudson. Follow me: @twostraws.