Jump to content

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?
Link to comment
https://forums.phpfreaks.com/topic/23200-to-echo-or-to-sprint/
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]
Link to comment
https://forums.phpfreaks.com/topic/23200-to-echo-or-to-sprint/#findComment-105188
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.