Page 128 - Beginning PHP 5.3
P. 128
Part II: Learning the Language
Using Type Specifiers
The d within the conversion specification, “%d” , is called a type specifier ; it tells printf() to format the
argument as a decimal integer. You can format in other ways using different type specifiers, as follows:
T ype S pecifier M eaning
b Treat the argument as an integer and format it as a binary number.
c Treat the argument as an integer and format it as a character with that ASCII
value.
d Treat the argument as an integer and format it as a signed decimal number.
e Format the argument in scientific notation (for example, 3.45e+2).
f Format the argument as a floating - point number, taking into account the
current locale settings (for example, many European locales use a comma for
the decimal point, rather than a period).
F Format the argument as a floating - point number, ignoring the locale
settings.
o Treat the argument as an integer and format it as an octal number.
s Format the argument as a string.
u Treat the argument as an integer and format it as an unsigned decimal
number.
x Treat the argument as an integer and format it as a lowercase hexadecimal
number.
X Treat the argument as an integer and format it as an uppercase hexadecimal
number.
% Display a literal percent ( % ) symbol. This doesn ’ t require an argument.
Here ’ s an example script that displays the same argument — the number 123.45 — formatted using
different type specifiers:
< !DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN”
“http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd” >
< html xmlns=”http://www.w3.org/1999/xhtml” xml:lang=”en” lang=”en” >
< head >
< title > Type Specifiers in Action < /title >
< link rel=”stylesheet” type=”text/css” href=”common.css” / >
< /head >
< body >
< h1 > Type Specifiers in Action < /h1 >
90
9/21/09 8:53:45 AM
c05.indd 90
c05.indd 90 9/21/09 8:53:45 AM