Have an account? Sign in
Login  Register  Facebook
This Page is Under Construction! - If You Want To Help Please Send your CV - Advanced Web Core (BETA)
Quick Table of Contents
  • print() — statement outputs data passed to it
  • echo() — statement for the same purposes as print()
  • printf() — output a blend of static text and dynamic information
  • sprintf() — identical to printf() except that the output is assigned to a string
[Edit] The printf() Statement
The printf() statement is ideal when you want to output a blend of static text and dynamic information stored within one or several variables. It’s ideal for two reasons. First, it neatly separates the static and dynamic data into two distinct sections, allowing for easy maintenance. Second, printf() allows you to wield considerable control over how the dynamic information is rendered to the screen in terms of its type, 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 static string:
printf("Please Wait %d seconds", 30);
Executing this command produces the following:
Please Wait 30 seconds
In this example, %d is a placeholder known as a type specifier, and the d indicates an integer value will be placed in that position. When the printf() statement executes, the lone argument, 100, will be inserted into the placeholder. Remember that an integer is expected, so if you pass along a number including a decimal value (known as a float), it will be rounded down to the closest integer. If you pass along 30.2 or 30.6, then 30 will be output. Pass along a string value such as “one hundred”, and 0 will be output, although if you pass along 123food, then 123 will be output. Similar logic applies to other type specifiers (see this Table for a list of commonly used specifiers).
TypeDescription
%bArgument considered an integer; presented as a binary number
%cArgument considered an integer; presented as a character corresponding to that ASCII
%dArgument considered an integer; presented as a signed decimal number
%fArgument considered a floating-point number; presented as a floating-point number
%oArgument considered an integer; presented as an octal number
%sArgument considered a string; presented as a string
%uArgument considered an integer; presented as an unsigned decimal number
%xArgument considered an integer; presented as a lowercase hexadecimal number
%XArgument considered an integer; presented as an uppercase hexadecimal number
September 12, 2011