13.09.2016 Views

PHP and MySQL Web Development 4th Ed-tqw-_darksiderg

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

112 Chapter 4 String Manipulation <strong>and</strong> Regular Expressions<br />

The %s in the format string is called a conversion specification.This one means “replace<br />

with a string.” In this case, it is replaced with $total interpreted as a string. If the value<br />

stored in $total was 12.4, both of these approaches would print it as 12.4.<br />

The advantage of printf() is that you can use a more useful conversion specification<br />

to specify that $total is actually a floating-point number <strong>and</strong> that it should have two<br />

decimal places after the decimal point, as follows:<br />

printf (“Total amount of order is %.2f”, $total);<br />

Given this formatting, <strong>and</strong> 12.4 stored in $total, this statement will print as 12.40.<br />

You can have multiple conversion specifications in the format string. If you have n<br />

conversion specifications, you will usually have n arguments after the format string. Each<br />

conversion specification will be replaced by a reformatted argument in the order they are<br />

listed. For example,<br />

printf (“Total amount of order is %.2f (with shipping %.2f) “,<br />

$total, $total_shipping);<br />

Here, the first conversion specification uses the variable $total, <strong>and</strong> the second uses the<br />

variable $total_shipping.<br />

Each conversion specification follows the same format, which is<br />

%[‘padding_character][-][width][.precision]type<br />

All conversion specifications start with a % symbol. If you actually want to print a %<br />

symbol, you need to use %%.<br />

The padding_character is optional. It is used to pad your variable to the width you<br />

have specified. An example would be to add leading zeros to a number like a counter.<br />

The default padding character is a space. If you are specifying a space or zero, you do not<br />

need to prefix it with the apostrophe (‘). For any other padding character, you need to<br />

prefix it with an apostrophe.<br />

The - symbol is optional. It specifies that the data in the field will be left-justified<br />

rather than right-justified, which is the default.<br />

The width specifier tells printf() how much room (in characters) to leave for the<br />

variable to be substituted in here.<br />

The precision specifier should begin with a decimal point. It should contain the<br />

number of places after the decimal point you would like displayed.<br />

The final part of the specification is a type code. A summary of these codes is shown<br />

in Table 4.1.<br />

Table 4.1<br />

Type<br />

b<br />

c<br />

d<br />

f<br />

o<br />

Conversion Specification Type Codes<br />

Meaning<br />

Interpret as an integer <strong>and</strong> print as a binary number.<br />

Interpret as an integer <strong>and</strong> print as a character.<br />

Interpret as an integer <strong>and</strong> print as a decimal number.<br />

Interpret as a double <strong>and</strong> print as a floating-point number.<br />

Interpret as an integer <strong>and</strong> print as an octal number.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!