Printing and Formatting Strings

Earlier we presented the basic method for outputting text-with echo and print-and the functions print_r( ) and var_dump( ), which can determine the contents of variables during debugging.

PHP provides several other functions that allow more complex and controlled formatting of strings.

Creating formatted output with sprintf( ) and printf( )

Sometimes more complex output is required than can be produced with echo or print. For example, a floating-point value such as 3.14159 might need to be truncated to 3.14 as it is output. For complex formatting, the sprintf( ) or printf( ) functions are useful:

string sprintf (string format [, mixed args...])
integer printf (string format [, mixed args...])

The operation of these functions is modeled on the identical C programming language functions, and both expect a string with optional conversion specifications, followed by variables or values as arguments to match any formatting conversions. The difference between sprintf( ) and printf( ) is that the output of printf( ) goes directly to the output buffer PHP uses to build a HTTP response, whereas the output of sprintf( ) is returned as a string.

Consider an example printf statement:

printf("Result: %.2f\n", $variable);

The format string Result: %.2f\nis the first parameter to the printf statement. Strings like Result: are output the same as with echo or print. The %.2f component is a conversion specification:

  • All conversion specifications begin with a % character.

  • The f indicates how the type of value should be interpreted. The f means the value should be interpreted as a floating-point number, for example, 3.14159 or 128.23765. Other possibilities include b, c, d, and s, where b means binary, c means a single character, d means integer, and s means string.

  • The .2 is an optional width specifier. In this example, .2 means two decimal places, so the overall result of %.2f is that a floating-point number with two decimal places is output. A specifier %5.3f means that the minimum width of the number before the decimal point should be five (by default, the output is padded on the left with space characters and right-aligned), and three digits should occur after the decimal point (by default, the output on the right of the decimal point is padded on the right with zeros).

In the example, the value that is actually output using the formatting string %.2f is the value of the second parameter to the printf function-the variable $variable.

To illustrate other uses of printf, consider the examples in Example 2-5.

Example 2-5. Using printf to output formatted data
   "-//W3C//DTD HTML 4.0 Transitional//EN"
   "" >
  <title>Examples of using printf(  )</title>
<body bgcolor="#ffffff">
<h1>Examples of using printf(  )</h1>
  // Outputs "3.14"
  printf("%.2f\n", 3.14159);
  // Outputs "      3.14"
  printf("%10.2f\n", 3.14159);
  // Outputs "3.1415900000"
  printf("%.10f\n", 3.14159);
  // Outputs "halfofthe"
  printf("%.9s\n", "halfofthestring");
  // Outputs "    3.14 3.141590       3.142"
  printf("%5.2f %f %7.3f\n", 3.14159, 3.14159, 3.14159);
  // Outputs "1111011 123 123.000000 test"
  printf("%b %d %f %s\n", 123, 123, 123, "test");