PHP sprintf: wtf?
Generally 'sprintf' is fine: it behaves as it supposed to, it does what you expect, it repeats its c implementation - everything seems to be fine. But there is one small thing which is not oresent in c: variables in c do not have prefix like in PHP.
So what? Why is it important? In most cases it is not important, but when you decide to use one argument for many values, it becomes crucial. In PHP you can put variable directly into string and the language will replace it with the value of variable:
$wtf = "wtf?"; echo "PHP sprintf: $wtf";It works only for double-quoted strings.
And when you write something like this:
printf("%1$s %1$s", "wtf?");PHP thinks you want to use variable $s inside printf(). But you don't — you just forgot how stupid it is (not always, but in some cases).
With single-quoted string as first argument (s)printf works just fine:
printf('%1$s %1$s', "wtf?");
But the question is still there: Why to use '$' symbol to point at number of argument to be used?