Forum Moderators: coopster
[edited by: eelixduppy at 3:23 pm (utc) on May 13, 2008]
[edit reason] removed url [/edit]
printf('binary: %b <br />char: %c <br /> int: %d <br /> float: %f <br />', 100, 100, 100, 100);
binary: 1100100
char: d
int: 100
float: 100.000000
See how neat that was? ;) hehe...ok not really, but it was a simple example. To do the above with concatenation you'd have to cast the value into each type and then echo it to the browser. Kind of annoying, but not really. It would get more annoying, however, if you wanted to format the string even further. Then you'd be left even using other functions sometimes to format the string how you could easily with a simple printf format string. Look at some of the examples at the sprintf page and you'll see how easy it can make some tasks. For another example (taken from the documentation) I like this one:
$s = 'monkey';
printf("[%s]\n", $s); // standard string output
printf("[%10s]\n", $s); // right-justification with spaces
printf("[%-10s]\n", $s); // left-justification with spaces
printf("[%010s]\n", $s); // zero-padding works on strings too
printf("[%'#10s]\n", $s); // use the custom padding character '#'
printf("[%10.3s]\n", $s); // left-justification but with a cutoff of 3 characters
Look at the output.