Formatting Strings
PHP provides a powerful way of creating formatted strings, using the printf and sprintf functions. If you have used this function in C, these will be quite familiar to you, although the syntax in PHP is a little different.
Using printf
You use printf to display a formatted string. At its very simplest, printf takes a single string argument and behaves the same as echo:
The power of printf, however, lies in its ability to substitute values into placeholders in a string. Placeholders are identified by the percent character (%), followed by a format specification character.The following example uses the simple format specifier %f to represent a float number.
printf("Hello, world");
The second argument to printf is substituted in place of %f, so the following output is produced:
$price = 5.99;
printf("The price is %f", $price);
There is actually no limit to the number of substitution arguments in a printf statement, as long as there are an equivalent number of placeholders in the string to be displayed. The following example demonstrates this by adding in a string item:
The price is 5.99
Table 6.1 shows the format characters that can be used with the printf function in PHP to indicate different types of values.
$item = "The Origin of Species";
$price = 5.99;
printf("The price of %s is %f", $item, $price);
| Character | Meaning |
|---|---|
| b | A binary (base 2) number |
| c | The ASCII character with the numeric value of the argument |
| d | A signed decimal (base 10) integer |
| e | A number displayed in scientific notation (for example, 2.6e+3) |
| u | An unsigned decimal integer |
| f | A floating-point number |
| o | An octal (base 8) number |
| s | A string |
| x | A hexadecimal (base 16) number with lowercase letters |
| X | A hexadecimal (base 16) number with uppercase letters |
In this case, PHP will treat the argument passed as an integer, so only the whole part of the value will be displayed. The output produced is as follows.
$price = 5.99;
printf("As a decimal, the price is %d", $price);
As a decimal, the price is 5
Format Codes
A format specifier can also include optional elements to specify the padding, alignment, width, and precision of the value to be displayed. This allows you to carry out some very powerful formatting.The width specifier indicates how many characters the formatted value should occupy in the displayed string and appears between the percent sign and the type specifier. For instance, the following example ensures that the name displayed takes up exactly 10 characters:
$name1 = "Tom";
$name2 = "Dick";
$name3 = "Harry";
echo "<PRE>";
printf("%10s \n", $name1);
printf("%10s \n", $name2);
printf("%10s \n", $name3);
echo "</PRE>";
The output produced is as follows:
$order = 201;
printf("Order number: %'05d", $order);
The precision specifier is used with a floating-point number to specify the number of decimal places to display. The most common usage is with currency values, to ensure that the two cent digits always appear, even in a whole dollar amount.The precision value follows the optional width specifier and is indicated by a period followed by the number of decimal places to display. The following example uses %.2f to display a currency value with no width specifier:
Order number: 00201
The price is correctly formatted as follows:
$price = 6;
printf("The price is %.2f", $price);
The price is 6.00
Using sprintf
The sprintf function is used to assign formatted strings to variables. The syntax is the same as for printf, but rather than being output as the result, the formatted value is returned by the function as a string.For example, to assign a formatted price value to a new variable, you could do the following:
All the format specifier rules that apply to printf also apply to sprintf.
$new_price = sprintf("%.2f", $price);