PhpDig.net

What is PhpDig?
PhpDig is a PHP MySQL based
Web Spider & Search Engine.




sprintf

Name

sprintf — Generates a formatted string.

Synopsis

string sprintf(format[, arg1]);
string format: Format string
mixed arg1 (optional): One or more arguments

Returns

String; FALSE on error

Description

sprintf() is used to generate formatted strings from one or more arguments. The format argument is a string consisting of normal text and/or special conversion specifications. Conversion specifications begin with a single percent symbol (%). A sample format string might look like this:

"There are %d days left to %s"

The normal text in format is sent unmodified to the output of the function, while each conversion specification should be matched by an additional argument to sprintf() . Continuing the previous example, a complete call to sprintf() using the format string just described might look like this:

$days  = 5;
$month = 'September';
echo sprintf("There are %d days left to %s", $days, $month);

Outputs:
There are 5 days left to September.


The conversion specifiers are very powerful. They provide convenient ways to format or transform the value of their corresponding arguments; see the following paragraphs for a full description.

Each conversion specifier starts with a single percent symbol%) and ends with a conversion character (one ofb, c,d,f,o,s, u,x, orX ). The specifier descriptions are described in the following table.

Conversion Character Description
b Convert the argument to an integer and display it as a binary number.
c Convert the argument to an integer and use the value as an ordinal value for a character.
d Convert the argument to an integer and display as a signed decimal value.
f Convert the argument to a float and display it as a floating-point number.
o Convert the argument to an integer and display it as an octal number.

Note

The value doesn't have a leading 0, as you might expect.

s Convert the argument to a string and display it as a string.
u Convert the argument to an unsigned integer and display it as an unsigned integer.
x Convert the argument to an integer and display it as hexadecimal number. Use lowercase letters to represent values greater than 9.

Note

The value doesn't have a leading 0x, as you might expect.

X Convert the argument to an integer and display it as hexadecimal number. Use uppercase letters to represent values greater than 9.

Note

The value doesn't have a leading 0x, as you might expect.



There may be additional conversion specifiers between the% and the conversion character. The following table lists the order in which they should be used within the conversion specifier.

Optional Conversion Specifiers Character(s) Description
Padding character specifier ' [^\0]

If the width of the conversion specifier is greater than the width of the provided string, the string is padded. By default, padding is added to the left end of the string with spaces.

The padding character specifier allows any single character other than NUL to be used to pad the string.

Alignment specifier - If present, field contents are aligned to the left instead of the right.
Width specifier [0-9]+ An integer number that specifies the width of the field.
Precision specifier .[0-9]+ A period, followed by an integer number that specifies the number of decimal digits for floating-point values. (This works only with %f.)


Version

PHP 3+, PHP 4+

See also

To send a string to stdout:

echo()

print()

printf()



Example

Example 1220. sprintf() demo

<?php
$values      = array(75, -10, 'one-hundred');
$conversions = array('b', 'c', 'd', 'f', 'o', 's', 'u', 'x', 'X');
$options     = array('', '12', '-12.4', "'x-12.4");

foreach($conversions as $conversion) {
   foreach($options as $option) {
      foreach($values as $value) {
         echo "\n$value processed with %$option$conversion:\n";
         echo sprintf("%$option$conversion\n", $value);
      }
   }
}
?>

Output:
75 processed with %b:
1001011

-10 processed with %b:
-1010

one-hundred processed with %b:
0

75 processed with %12b:
     1001011

-10 processed with %12b:
       -1010

one-hundred processed with %12b:
           0

75 processed with %-12.4b:


-10 processed with %-12.4b:


one-hundred processed with %-12.4b:


75 processed with %'x-12.4b:
xxxxxxxxxxxx

-10 processed with %'x-12.4b:
xxxxxxxxxxxx

one-hundred processed with %'x-12.4b:
xxxxxxxxxxxx

75 processed with %c:
K

-10 processed with %c:
’

one-hundred processed with %c



PHP Functions Essential Reference. Copyright © 2002 by New Riders Publishing (Authors: Zak Greant, Graeme Merrall, Torben Wilson, Brett Michlitsch). This material may be distributed only subject to the terms and conditions set forth in the Open Publication License, v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/). The authors of this book have elected not to choose any options under the OPL. This online book was obtained from http://www.fooassociates.com/phpfer/ and is designed to provide information about the PHP programming language, focusing on PHP version 4.0.4 for the most part. The information is provided on an as-is basis, and no warranty or fitness is implied. All persons and entities shall have neither liability nor responsibility to any person or entity with respect to any loss or damage arising from the information contained in this book.

Powered by: vBulletin Version 3.0.7
Copyright ©2000 - 2005, Jelsoft Enterprises Ltd.
Copyright © 2001 - 2005, ThinkDing LLC. All Rights Reserved.