Using printf

The printf function works by taking as its first parameter a special formatting string. The formatting string works like a template to describe how to plug the rest of the parameters into one resulting string. You can specify details such as how to format numbers in the string or the padding of values. Each parameter that's placed into the resulting string has a placeholder in the formatting string. For example, to output a binary number, use the code in Example 11-1.

Example 11-1. Displaying a number in binary format <?php printf("The computer stores the number 42 internally as %b.",42); ?>

This code then produces the output shown in Figure 11-1.

Easiis^HEEr -injxi gle Edit View Go Bookmarks Tools help

The computer stores the number 42 internally as 101010.

1 1

Figure 11-1. Displaying 42 in binary format

Figure 11-1. Displaying 42 in binary format

The formatting string in Example 11-1 contains a placeholder that specifies where to put the second parameter of 42. It begins with a percent sign (%), which is called the conversion specification. There can be any number of conversion specifications in the formatting string, but they must each have a corresponding parameter when printf is called.

The character after the percent sign is the type specifier. The type specifier defines how the parameter is formatted for display when it's placed in the output string, as demonstrated in Example 11-2.

Example 11-2. printf puts the numbers into the string <?php printf("The computer stores the numbers 42, and 256 internally as %b and %b.", 42,256);

When called from a web browser, the code in Example 11-2 displays Figure 11-2.

Hie Edit View Go Bookmarks Tools Help

The computer stores the numbers 42, and 256 internally as 101010 and 10000000C

1 1

Figure 11-2. Including two numbers in the string

Figure 11-2. Including two numbers in the string

So far, the only type specifier we've used is b for binary, but there are more. Table 11-1 lists numeric type specifiers.

Table 11-1. Type specifiers for numbers

Specifier Meaning Example (using 42)

d Display as a decimal number 42

b Display as a binary number 101010

c Display as ASCII equivalent *

f Display as a floating-point number, double precision 42.000000

o Display as an octal number, base 8 52

s Display as a string 42

x Display as a lowercase hexadecimal 2a

X Display as an uppercase hexadecimal 2A

The last column of Table 11-1 was generated with the code in Example 11-3.

Example 11-3. Displaying the same number in different formats

<?php $value=42; printf("%d<br /> printf("%b<br /> printf("%c<br /> printf("%f<br /> printf("%o<br /> printf("%s<br /> printf("%x<br />

",$value) ",$value) ",$value) ",$value) ",$value) ",$value) ",$value) ",$value)

Example 11-3 gives you this column:



In practice, you might use this code to convert from an integer to a hexadecimal number if you're building a string when specifying colors in HTML elements. Since you tend to relate better to the decimal value, you can use decimals and have them automatically formatted correctly for display in a tag such as color="#2a11cc".

Was this article helpful?

0 0

Post a comment