|
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 printfYou use printf to display a formatted string. At its very simplest, printf takes a single string argument and behaves the same as echo: printf("Hello, world"); 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. $price = 5.99; printf("The price is %f", $price); The second argument to printf is substituted in place of %f, so the following output is produced: The price is 5.99 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: $item = "The Origin of Species"; $price = 5.99; printf("The price of %s is %f", $item, $price); Table 6.1 shows the format characters that can be used with the printf function in PHP to indicate different types of values.
Suppose you use the %d format specifier instead of %f to display the value of $price: $price = 5.99; printf("As a decimal, the price is %d", $price); 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. As a decimal, the price is 5
Format CodesA 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>";
If you run this example through a web browser, you will see that each name displayed is indented from the left of the screen by the correct number of characters to make each name right-aligned with the others. The default behavior is to right-align to the given width. However, you can reverse this by using the minus symbol as an alignment specifier. To left-align the strings in the previous example, you would use the format specifier %-10s. Although visibly this would not appear any different from simply using %s, the strings would be padded on the right with spaces to a length of 10 characters. You can change the padding character from a space to any other character by placing that character before the width value, prefixed with a single quotation mark. The following example ensures that a five-digit order number is always displayed padded with zeros if necessary: $order = 201; printf("Order number: %'05d", $order); The output produced is as follows: Order number: 00201 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: $price = 6; printf("The price is %.2f", $price); The price is correctly formatted as follows: The price is 6.00
Using sprintfThe 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: $new_price = sprintf("%.2f", $price); All the format specifier rules that apply to printf also apply to sprintf. |
|