Returns a string produced according to the formatting string
format.
The format string is composed of zero or more directives:
ordinary characters (excluding %) that are
copied directly to the result, and conversion
specifications, each of which results in fetching its
own parameter. This applies to both sprintf()
and printf().
Each conversion specification consists of a percent sign
(%), followed by one or more of these
elements, in order:
An optional sign specifier that forces a sign
(- or +) to be used on a number. By default, only the - sign is used
on a number if it's negative. This specifier forces positive numbers
to have the + sign attached as well, and was added in PHP 4.3.0.
An optional padding specifier that says
what character will be used for padding the results to the
right string size. This may be a space character or a
0 (zero character). The default is to pad
with spaces. An alternate padding character can be specified
by prefixing it with a single quote (').
See the examples below.
An optional alignment specifier that says
if the result should be left-justified or right-justified.
The default is right-justified; a -
character here will make it left-justified.
An optional number, a width specifier
that says how many characters (minimum) this conversion should
result in.
An optional precision specifier that says
how many decimal digits should be displayed for floating-point
numbers. When using this specifier on a string, it acts as a
cutoff point, setting a maximum character limit to the string.
A type specifier that says what type the
argument data should be treated as. Possible types:
% - a literal percent character. No
argument is required.
b - the argument is treated as an
integer, and presented as a binary number.
c - the argument is treated as an
integer, and presented as the character with that ASCII
value.
d - the argument is treated as an
integer, and presented as a (signed) decimal number.
e - the argument is treated as scientific
notation (e.g. 1.2e+2).
u - the argument is treated as an
integer, and presented as an unsigned decimal number.
f - the argument is treated as a
float, and presented as a floating-point number (locale aware).
F - the argument is treated as a
float, and presented as a floating-point number (non-locale aware).
Available since PHP 4.3.10 and PHP 5.0.3.
o - the argument is treated as an
integer, and presented as an octal number.
s - the argument is treated as and
presented as a string.
x - the argument is treated as an integer
and presented as a hexadecimal number (with lowercase
letters).
X - the argument is treated as an integer
and presented as a hexadecimal number (with uppercase
letters).
As of PHP 4.0.6 the format string supports argument
numbering/swapping. Here is an example:
Example 1. Argument swapping
<?php $format = "There are %d monkeys in the %s"; printf($format, $num, $location); ?>
This might output, "There are 5 monkeys in the tree". But
imagine we are creating a format string in a separate file,
commonly because we would like to internationalize it and we
rewrite it as:
We now have a problem. The order of the placeholders in the
format string does not match the order of the arguments in the
code. We would like to leave the code as is and simply indicate
in the format string which arguments the placeholders refer to.
We would write the format string like this instead:
printf("[%s]\n", $s); // standard string output printf("[%10s]\n", $s); // right-justification with spaces printf("[%-10s]\n", $s); // left-justification with spaces printf("[%010s]\n", $s); // zero-padding works on strings too printf("[%'#10s]\n", $s); // use the custom padding character '#' printf("[%10.10s]\n", $t); // left-justification but with a cutoff of 10 characters ?>
sprintf php code on this is provided for your study purpose, it will guide you to know how create and design a website using php. use it to practice and train your self online
Php sprintf syntax tutorial
php tutorial guide and code design are for easy learning and programming. The code practice section provided at the top is for practising of this syntax. Use the code section up to practice your php programming online. Learning php is very easy, all you need is to use the examples on this site and practice them to perfect your skills.