Zane Posted October 6, 2006 Share Posted October 6, 2006 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 echowhat 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 endI could just as easily say echo "My name is {$name}";instead ofsprintf("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 More sharing options...
obsidian Posted October 6, 2006 Share Posted October 6, 2006 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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.