You can re-use or repeat an argument in PHP's
sprintf, for example, like so:
echo sprintf('I found %1$d %2$s! Hello %2$s!', 10, 'ducks'); // output: "I found 10 ducks! Hello ducks!"
From the example above, you may have deduced that the format for re-using an argument in PHP's
sprintf is as follows:
Where argnum denotes the position of the argument you wish to re-use or repeat, and specifier denotes the type (from one of the following):
|Specifier||Treated As||Presented As|
||integer||Signed decimal number|
||scientific notation||Scientific notation with lowercase "e" (e.g. 1.2e+2)|
||scientific notation||Scientific notation with uppercase "E" (e.g. 1.2E+2)|
||float||Floating-point number (locale aware)|
||float||Floating-point number (non-locale aware)|
||integer||Unsigned decimal number|
||integer||hexadecimal number (with lowercase letters)|
||integer||hexadecimal number (with uppercase letters)|
%2$d would mean, "use the second argument after the string we need to format, and display it as a decimal number".
Use single quotes for the string you need to format, otherwise PHP will treat the specifier after the
$ as a string.
Did you find this article useful? Or do you have something to add to it? Please let us know in the comments section.