11.07.2015 Views

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

PHP MySQL - Stilson.net

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

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

CHAPTER 3 • <strong>PHP</strong> BASICSwww.it-ebooks.infowield considerable control over how the dynamic information is rendered to the screen in terms of itstype, precision, alignment, and position. Its prototype looks like this:integer printf(string format [, mixed args])For example, suppose you wanted to insert a single dynamic integer value into an otherwise staticstring:printf("Bar inventory: %d bottles of tonic water.", 100);Executing this command produces the following:Bar inventory: 100 bottles of tonic water.In this example, %d is a placeholder known as a type specifier, and the d indicates an integer valuewill be placed in that position. When the printf() statement executes, the lone argument, 100, will beinserted into the placeholder. Remember that an integer is expected, so if you pass along a numberincluding a decimal value (known as a float), it will be rounded down to the closest integer. If you passalong 100.2 or 100.6, then 100 will be output. Pass along a string value such as “one hundred”, and 0 willbe output, although if you pass along 123food, then 123 will be output. Similar logic applies to other typespecifiers (see Table 3-1 for a list of commonly used specifiers).Table 3-1. Commonly Used Type SpecifiersTypeDescription%b Argument considered an integer; presented as a binary number%c Argument considered an integer; presented as a character corresponding to that ASCII value%d Argument considered an integer; presented as a signed decimal number%f Argument considered a floating-point number; presented as a floating-point number%o Argument considered an integer; presented as an octal number%s Argument considered a string; presented as a string%u Argument considered an integer; presented as an unsigned decimal number%x Argument considered an integer; presented as a lowercase hexadecimal number%X Argument considered an integer; presented as an uppercase hexadecimal numberSo what if you’d like to pass along two values? Just insert two specifiers into the string and make sureyou pass two values along as arguments. For example, the following printf() statement passes in aninteger and float value:48

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

Saved successfully!

Ooh no, something went wrong!