I get the error
Warning: printf() [function.printf]: Too few arguments
Looking at the code I see:
function twentyten_posted_on() {
printf(
sprintf( '<a href="%1$s" title="%2$s" rel="bookmark"><span class="entry-date">%3$s</span></a>',
get_permalink(),
esc_attr( get_the_time() ),
get_the_date()
)
);
}
What seems to be the problem?
Printf can take as many arguments as you want. In the man page you can see a ... at the end, which stands for a var args. If you got 96 times %s in your first argument, you'll have 97 arguments (The first string + the 96 replaced strings ;) )
Definition and Usage. The printf() function outputs a formatted string. The arg1, arg2, ++ parameters will be inserted at percent (%) signs in the main string. This function works "step-by-step".
The printf()
and sprintf()
functions are identical; the only difference in their behaviour is that one is a statement (it does something) and the other is a function expression (it evaluates to something). (See this StackOverflow answer for a description of the difference.) They both take a format as the first argument, then zero or more additional arguments as replacement strings for special characters within the format string.
Your sprintf()
function is well formed. You've numbered your format strings, you have replacement strings as arguments to match the format strings. All is good.
But consider what the printf()
function is doing. It gets a string, which happens to be the output of sprintf()
. If sprintf()
's contains a %
character, then printf()
would need a replacement string which is not included in your code.
As others have said, you can probably leave out the sprintf()
from your code. But you should also understand WHY this is happening.
For example:
$fmt = "%%d\n";
printf( $fmt );
printf( sprintf($fmt) );
printf( sprintf($fmt), "Hello world" );
The first printf works, and prints a "%d". The second printf fails because its format string implies that it should have a replacement string, but none is provided. The third one prints a zero, because when you try to evaluate "Hello world" as a decimal integer (%d
), that's what you get.
Look at your variables, and you'll probably find that at least one of them has a %
character in it.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With