Online
 
Friday, 09 January 2009
 
 
More article:
Related Content:

Formatting Strings
 
Article Index
Formatting Strings
Page 2
Page 3
Page 4

%[P][-]W[.R]T 

T is the type of the parameter (see ), W is the minimum length that the data should take in the output string, P is an optional padding character to be used as a filler to ensure that the data takes at least W characters.

Table 1.4. printf() Type Specifiers

Option

Value

%

A literal percent characters (takes no parameters)

b

Integer represented as a binary number (for example: 101110111)

c

Integer represented as the character corresponding to its ASCII value

d

Integer represented as a signed integer number

u

Integer represented as an unsigned number

f

Floating-point value

o

Integer represented as an octal value

s

String value

x

Integer value represented in hexadecimal notation (with lowercase characters)

X

Integer value represented in hexadecimal notation (with uppercase characters)

 

R is an optional precision token that has meaning only when dealing with floating-point values; it specifies the number of decimal digits that should be used to represent the data.

Finally, a dash (-) placed strategically between P and W indicates that the data should be left-aligned in the space allotted to it by W.

This all sounds a lot more complicated than it really is. Let's take a look at a few examples:

%-5d

This token represents a right-aligned integer value that must be at least five characters long.

%05.3f

This token represents a floating-point value at least five characters long and with no less than three decimal digits. The character "0" is used to pad the string to its minimum length.

The printf function makes it relatively easy to format complex strings using a single expression. Here's an example:

<?php

$n = 15.32;
$log = log ($n);

printf ("log (%0.2f) = %.5f\n", $n, $log);

?>

Tags: Add more tags...,
This entry was posted on . You can follow any responses to this entry through the RSS 2.0 feed. You can leave a comment. Tags: Simple PHP, Pear, Easy PHP, PHP Tutorial, PHP MySQL, XSLT, Sap Tutorial, CSS Tutorial, XSL FO Java, SQL Tutorial.
Users' Comments (0)

Comment an article
  Name
  E-mail
   Title
Available characters: 4000
 Notify me of follow-up comments
This image contains a scrambled text, it is using a combination of colors, font size, background, angle in order to disallow computer to automate reading. You will have to reproduce it to post on my homepage
Enter what you see:

No comment posted

Cool Graphic Wallpaper 50
Statistic


Last Post

 
Top! Top!