Jump to content

Archived

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

Zane

to echo or to sprint

Recommended Posts

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?

Share this post


Link to post
Share on other sites
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:
[code]
<?php
$num = 134;
printf("My number is %06d", $num);
?>
[/code]

Share this post


Link to post
Share on other sites

×

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.