Jump to content

[SOLVED] problem with sprintf


sitorush

Recommended Posts

Hello friends, i have problem with sprintf

$image = '<div align="center"><a href="images/'.$gambar.'/%%s" target="_blank"><img src="'.$phpThumbBase.'./images/'.$gambar.'/%s&aoe=1&w='.$width.'" alt=""></div>';

in the loop...

echo '<td>'.(isset($chart['tampil'][$row][$col])? sprintf($image,$chart['tampil'][$row][$col]) : '').'</td>';

sprintf wont print $image if i put %s twice it says Warning: sprintf() [function.sprintf]: Too few arguments in C:\wamp\www\phpthumb\incl\catalog8.php on line 31 but if i put %%s in front it will work but can't make as link (bad request).

can you help me friends... thanks:)

tom

Link to comment
https://forums.phpfreaks.com/topic/52583-solved-problem-with-sprintf/
Share on other sites

What are you trying to do exactly? Your code is a mess.

 

Your only passing one parameter to sprintf yet you have several (attempts at) placeholders.

 

With all those variables in the string your passing to sprintf I'm not sure you understand what sprintf is even for. Take a look at the man.

 

ps; please use the [ code ] [/ code ] tags (without the space), it makes code MUCH easier to read.

Here's an example

 

$i = 0;
while ($i < 10) {
  $str = sprintf("[%s] %s\n", $i, "This is row $i");
  print $str;
  $i++;
}

 

The % placeholders match the order of the arguments.  So the first one is replaced by $i, and the second replaced by "This is row $i".

 

It's usually a bad idea to mix "." style and sprintf() style.  They are two different ways to accomplish the same thing.

I was intending to show the difference between using a variable directly, and using a string :)

 

I think that using "string $var" or "." style in the arguments to sprintf is fine, just not in the format string.  Using it in the format string is missing the point of sprintf altogether.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.