13.09.2016 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Formatting Strings<br />

113<br />

Table 4.1<br />

Type<br />

s<br />

u<br />

x<br />

X<br />

Continued<br />

Meaning<br />

Interpret as a string <strong>and</strong> print as a string.<br />

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

Interpret as an integer <strong>and</strong> print as a hexadecimal number with lowercase letters for<br />

the digits a–f.<br />

Interpret as an integer <strong>and</strong> print as a hexadecimal number with uppercase letters for<br />

the digits A–F.<br />

When using the printf() function with conversion type codes, you can use argument<br />

numbering.That means that the arguments don’t need to be in the same order as the<br />

conversion specifications. For example,<br />

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

$total_shipping, $total);<br />

Just add the argument position in the list directly after the % sign, followed by an escaped<br />

$ symbol; in this example, 2\$ means “replace with the second argument in the list.”This<br />

method can also be used to repeat arguments.<br />

Two alternative versions of these functions are called vprintf() <strong>and</strong> vsprintf().<br />

These variants accept two parameters: the format string <strong>and</strong> an array of the arguments<br />

rather than a variable number of parameters.<br />

Changing the Case of a String<br />

You can also reformat the case of a string.This capability is not particularly useful for the<br />

sample application, but we’ll look at some brief examples.<br />

If you start with the subject string, $subject, which you are using for email, you can<br />

change its case by using several functions.The effect of these functions is summarized in<br />

Table 4.2.The first column shows the function name, the second describes its effect, the<br />

third shows how it would be applied to the string $subject, <strong>and</strong> the last column shows<br />

what value would be returned from the function.<br />

Table 4.2 String Case Functions <strong>and</strong> Their Effects<br />

Function Description Use Value<br />

$subject<br />

Feedback from<br />

web site<br />

strtoupper() Turns string to strtoupper($subject) FEEDBACK<br />

uppercase<br />

FROM<br />

WEB SITE<br />

strtolower() Turns string to strtolower($subject) feedback from<br />

lowercase<br />

web site<br />

ucfirst() Capitalizes first ucfirst($subject) Feedback from<br />

character of string<br />

web site<br />

if it’s alphabetic

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

Saved successfully!

Ooh no, something went wrong!