Read the PHP docs on sprintf().
%s
is just a placeholder for a string%d
is just a placeholder for a number
So an example of sprintf would look like this:
$variable = sprintf(
'The %s ran down the %s', // String with placeholders
'dog', // Placed in the first %s placeholder
'street' // Placed in the second %s placeholder
);
Which will return a string to our variable $variable
:
The dog ran down the street
By numbering the placeholders it’s both a developer-friendly way to quickly tell which following string will be placed where. It also allows us to reuse a string. Let’s take another example with numbered placeholders:
$variable = sprintf(
'The %1$s ran down the %2$s. The %2$s was made of %3$s', // String with placeholders
'dog', // Will always be used in %1$s placeholder
'street', // Will always be used in %2$s placeholder
'gravel' // Will always be used in %3$s placeholder
);
Which will return a string to our variable $variable
:
The dog ran down the street. The street was made of gravel
Finally, the __()
function let’s us translate strings passed to it. By passing __()
placeholders, and then passing that whole string to sprintf()
, we can translate whatever is passed to the translating function allowing us to make our string and application a bit more dynamic.