PHP function of the day: printf()

For day 16 of 21 in the “PHP Function of the day” series, I’m going to look at printf(). This one is pretty interesting.  It allows you to define a format and a series of arguments.  The “%[something]” in the format string is used to define both where the argument’s value is output and the expected format.  In the format string below, “%s” is a string, “%u” is an unsigned decimal number, and “%%” is simply a percentage symbol.

Let’s take a look.

<?php

$name = “Joe”;

$food = “Chicken”;

$percentHungry = 72;

printf(“My name is %s and I am %u%% hungry for %s.”,$name,$percentHungry,$food);

?>

 

See the output

 

There are a variety of possible format values.  It is worth looking up all of the options.

Leave a Reply

Your email address will not be published.