The printf() function formats an input and immediately prints
it.
When formatting the input we should not forget that special characters are
not counted only as a single character! This also applies to the output.
Procedurálne
functionprintf (string$format, mixed$args, mixed$...) : int
Parametre
Názov
Dátový typ
Predvolená hodnota
Popis
$format
string
The format of a string. Ordinary characters described below represent
variables which must be passed in the exact order after a percent sign (%).
An optional parameter. It is a sign character (- or +) specifying if the
number must have the sign character attached. The default character is -; that
means only negative numbers have the sign. Otherwise, if the + sign is added,
the numbers have always the sign character.
An optional character used in case of padding the result (e.g. if the string
is not long enough). Space or 0 can be used as well as any character preceded by
an apostrophe (').
A dash (-) that says how a string is padded. If the dash is added, the short
string is padded from the right (the default is the left).
An optional number specifying the minimum length of a string.
An optional parameter with a number after a period (.). It defines how many
decimal digits a number will have (they are either cut off or zeros are added).
It can also be used for strings where the maximum length is then given.
A required parameter specifying what type the argument data
should be treated as. Possible types:
% - a percent character. No argument is needed.
b - converts an integer to a binary number.
c - converts an integer to an ASCII character.
d - an integer.
e - the value is presented in the scientific notation (e.g. 1.2e+2).
E - the same as %e but uses the uppercase letter instead (e.g.
1.2E+2).
f - a decimal number (locale aware).
F - a decimal number (non-locale aware).
g - shorter of %e and %f.
G - shorter of %E and %f.
o - converts an integer to an octal number.
s - a string.
u - converts an integer to an unsigned number.
x - converts an integer to a hexadecimal number (lowercase letters).
X - converts an integer to a hexadecimal number (uppercase letters).
$args
mixed
Arguments representing variables in the format string.
$...
mixed
Arguments representing variables in the format string.
Mávratovej hodnoty
Vracia: int
The length of the outputted string.
Príklady
<?phpprintf("Hello %s!", "world");
We can also add more parameters when they are inserted in the specified
order:
<?phpprintf("%s were %d bananas lying", "There", 5);
We can also change the order:
<?phpprintf('%2$s were %1$d bananas lying', 5, "There");
It is also possible to add the selected character to numbers to a given
length: