Jump to content


Photo

to echo or to sprint


  • Please log in to reply
1 reply to this topic

#1 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 06 October 2006 - 06:10 PM

A bit odd of a question for me..

I see people using sprintf() a lot in their code.
Which in turn you could do the same thing with echo

what is so good about sprintf and printf as opposed to echo or print.

Cuz all I recognize from sprintf is that you put you're placeholders and then you're variables at the end
I could just as easily say
echo "My name is {$name}";
instead of
sprintf("My name is %s", $name);

What are the benefits?

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#2 obsidian

obsidian
  • Staff Alumni
  • Advanced Member
  • 3,202 posts
  • LocationSeattle, WA

Posted 06 October 2006 - 06:27 PM

the major thing that i like about printf and sprintf (when i use them) is that you can actually format your variables on the fly. for instance, if i want to enter all my numerics as 6 digit numbers, i would want to left-pad them with zeros to make numbers like 134, 20 and 5 work, thus i could solve it simply with printf:
<?php
$num = 134;
printf("My number is %06d", $num);
?>

You can't win, you can't lose, you can't break even... you can't even get out of the game.

<?php
while (count($life->getQuestions()) > 0)
{   $life->study(); } ?>
  LINKS: PHP: Manual MySQL: Manual PostgreSQL: Manual (X)HTML: Validate It! CSS: A List Apart | IE bug fixes | Zen Garden | Validate It! JavaScript: Reference Cards RegEx: Everything RegEx




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users