printf("by %1$s on %2$s", 'string1', 'string2');
doesn’t work, whereas printf('by %1$s on %2$s', 'string1', 'string2');
does.
I’m actually designing a WordPress theme, and following the original twentyten theme very closely. The weird thing is, I’ve been using double quotation marks on all my previous printf() statements without any problems whatsoever.
Because when you are using double quotes the
$s
is treated as a variableAs in:
where as when using:
For a more detailed explanation you can check the manual:
Strings in General
Single quoted vs Double quoted
It is very important to realize php is treating single quoted and double quoted strings differently.
You can read more in official php docs, but let me give you a highlight:
will output $t, where
will output bla
That’s because you have the ‘$s’ bit in your string. When using double quotation marks PHP interprets it as a variable and tries to parse it. You probably used double quotations without the
$
in it earlier.As the other answers say, it treats
$s
as a variable, you could always escape the$
I would however use single quotation marks, as php doesn’t need to parse the string and is therefore faster.