This is an article I wrote a while ago on my old blog when I first started learning PHP. I'll repost it here, in case it helps anyone :)
printf() function lets you print something in a specific format. For example, you can format prices to make sure they always show up with only two decimal places instead something like
$4.539999, or you can make a template string that would allow you to stick in different variables but keep the same format each time you need to display it (e.g. make a template for displaying lines on a menu).
It took me a couple of tries before I got it. At first, I was thinking it involves dealing with too many cryptic symbols and coded letters, but after you know a few of the basic ones, I saw how it could be useful.
So, the basic format is
printf(<format template for string>, <variable to plug in>, <another variable to plug in>, ...)
The first argument, the format template for your string, is like a normal string but it uses special codes that are replaced by the plugin variables. The second argument and on are the variables you want to plug in. You can have as many as you want, but they should be in the order that they appear in the format template string.
For example, one of the most common uses of
printf() is to format prices:
$tofu = 1.30; printf("\$%.2f", $tofu * 2); // Output $2.60
Okay, so what is going on with that
\$ is going to just put a dollar sign in front of your price. As for
%.2f, this is what formats your price. The
% indicates that the formatted variable is here and f indicates that this is a floating point number (number with decimal points). For floating points, you indicate how many decimal points you want with the
.2, which in this case makes sure the number has 2 decimal places.
In the example above, if you just multiplied the price of tofu by two, the number would end up being 2.6, so I’m using the format to make sure that a zero will be added in cases like this.
Here’s another, slightly more complicated example. To keep things clean and to avoid having to type out those annoying format strings, you can save the format string into its own variable and then plug into into the
printf() function, like the
$price_format variable below:
$eggs = 1.99; $tofu = 1.29; $beer = 8.99; $price_format = "\$%.2f\n"; print "Receipt\n"; printf("Eggs $price_format", $eggs * 2); printf("Tofu $price_format", $tofu); printf("Beer $price_format", $beer * 2); printf("Total $price_format", ($eggs * 2) + $tofu + ($beer * 2)); // Output: Receipt Eggs $3.98 Tofu $1.29 Beer $17.98 Total $23.25
You can also use
%s to add a variable that is a string into your format. For example, if we want to also plug in the names of the grocery items into the format string:
$eggs = 1.99; $tofu = 1.29; $beer = 8.99; $price_format = "%s \$%.2f\n"; print "Receipt\n"; printf($price_format, 'Eggs', $eggs * 2); printf($price_format, 'Tofu', $tofu * 1); printf($price_format, 'Beer', $beer * 2); printf($price_format, 'Total', ($eggs * 2) + $tofu + ($beer * 2)); // Output Receipt Eggs $3.98 Tofu $1.29 Beer $17.98 Total $23.25
Another example of using
%s to plug in string variables, such as in a website’s title bar (though, there are probable more elegant ways to do this):
$site_title = "Recipe Box"; $page_title = "Grandma's Apple Pie"; $title_format = "%s | %s"; printf($title_format, $site_title, $page_title); // Output Recipe Box | Grandma's Apple Pie
Because you can indicate the length of a particular variable and include leading characters if needed,
printf() is also handy for formatting dates:
$month = 1; $day = 24; $year = 2017; $date = sprintf("%02d/%02d/%04d", $month, $day,$year); echo $date; // Output 01/24/2017
In this format string,
"%04d/%02d/%02d", the d is used to indicate the variable type is an integer (there are also other types, but this is the basic one). The
2s are telling the function that this number is 4 digits or 2 digits long, while the 0s are the “padding character” that will be added in front of the number isn’t long enough. So, for the month of January (
1) in this case, it gets a 0 stuck in front of it in the printed string
Some other things you can do is format numbers to include positive and negative signs (e.g. for temperatures), easily output scientific notations, and more. Check out more of the formatting options on the PHP Manual sprintf page.