PHP Strings Primer - printf
(Page 10 of 37 )
The last two techniques for displaying data that we discussed were very similar in nature. The 'printf' function, on the other hand, is very different from either echo or print though it may seem comparable on the surface. The first difference is that it is indeed a function, unlike the first two techniques which were language constructs.
In its very basic use, 'printf' does work just as the first two display methods we looked at. Take for instance this example:
With this particular example, we could exchange 'echo' or 'print' for the 'printf()' statement and it would essentially work just the same. Do note however that 'printf()' requires parentheses unlike either of the other two techniques due to the fact that it is a function and not a language construct.
The major differences with 'printf()' come when we want to provide formatted output of variables. As we saw earlier, with 'echo' and 'print' we can just include the variable name within a double quoted string and it will be outputted, although we have no control over its format. We can do the same with 'printf()', but we can also take it a step further. The 'printf()' function allows us to put placeholders in our output string and then pass the variables in as additional arguments. We will see in a moment how this allows us to tap into the power of printf()', but first let's just look at a simple example.
<?php $var = 'Hello'; printf ("She said '%s' to me.", $var); ?> |
This will output:
The value we passed has been placed into the string at the point we put the placeholder. In this case we used a placeholder of '%s'. This signifies that we want to output a string. Other place holders include '%d' for integers and '%f' for floating point numbers. In order to include a literal percent symbol, we must use a double percent symbol, '%%'.
The real power of 'printf()', however, is in its ability to format the string before outputting it. A common use of the formatting abilities of printf is to pad a number for display. This allows you to be sure that your output is consistent. In the following example, we will output a number and pad it with zeros to ensure that it displays as five characters.
<?php $var = "12"; printf ("A padded number: %05d", $var); ?> |
This piece of code will output the following:
As you can see, the number twelve is displayed with three zeros preceding it. If the number had been single digit, rather than double digit, four zeros would have preceded it. Using this method to pad numbers, we can produce consistent and easy to read output.
Another formatting capability of 'printf()' is to specify the amount of significant digits displayed for floating point numbers. Again, this can be useful for readability and maintaining conformity within output. Let's look at another bit of code that uses this functionality.
<?php $var = 1.56823; printf ("This number has 2 significant digits: %.2f", $var); ?> |
This will output:
This number has 2 significant digits: 1.57 |
Notice how it only displays two significant digits? Also, pay special attention to the fact that 'printf()' rounded the number up for us, rather than just dropping the extra digits.
The 'printf()' function is not nearly as efficient, time-wise, as echo or print, but the benefits gained from formatted output can make choosing this function simple if the functionality is needed. Here we have just taken a quick tour of the 'printf()' functions capabilities, we implore you to take a closer look into its functionality.
Another function that works almost identically to 'printf()' is the 'sprintf()' function. The only difference between the two is that 'sprintf()' will return the string, so that you may store it in a variable or use it in another function, rather than display it as 'printf()' does.
Next: Strings Formatting >>
More Programming Basics Articles
More By Matt Wade