Microsoft Word - Core PHP Programming Using PHP to Build Dynamic Web Sites

(singke) #1

You also have the option of placing characters between the % and the type specifier that
control how the data is formatted. Immediately following the % you may place any
number of flags. These flags control padding and alignment. They are listed in Table 8.2.


After any flags, you may specify a minimum field length. The converted output will be
printed in a field at least this wide, longer if necessary. If the output is shorter than the
minimum width, it will be padded with a padding character, a space by default. The
padding will normally be placed to the left but, if the - flag is used, it will be placed to the
right.


Next, you may specify a precision. It must start with a period to separate it from the
minimum field length. For strings, the precision is taken to mean a maximum field
length. For doubles, the precision is the number of digits that appear after the decimal
point. Precision has no meaning for integers.


Table 8.1. printf Type Specifiers
Type
Specifier Description^

d (^) Integer, decimal notation.
o (^) Integer, octal notation.
x, X Integer, hexadecimal notation. "x" will use lowercase letters; "X" will use uppercase letters.
b (^) Integer, binary notation.
c Character specified by integer ASCII code. See Appendix B for a complete
list of ASCII codes.
s (^) String.
f (^) Double.
e (^) Double, using scientific notation such as 1.2e3.
% Print a percentage sign. This does not require a matching argument.
<?
printf("%-10s %5d %05.5f
\n", "a string", 10, 3.14);
?>
Output Buffering
The output buffering commands add a layer of buffering controlled by PHP in addition to
whatever buffering the Web server uses. Some performance penalty may be incurred by
adding another layer of buffering, but you may decide the greater control you have is
worth the price.
When ob_start is called, all output by functions such as print and echo are held back
in a buffer, a large area of memory. The contents of the buffer may be sent to the browser
using ob_end_flush, or it may be thrown away using ob_end_clean. As you recall from
Chapter 7, "I/O and Disk Access," headers cannot be sent after the first content is sent.

Free download pdf