Difference between PRINT, printf and echo

What is the difference between PRINT, printf and echo?

They are all used to print text on the screen but printf function is way more complicated.

echo seems to be the simplest to use why would anyone use printf or PRINT

I believe print is simply for text, printf for Formatting text with some HTML markup (F = Formatting), and echo can be for anything.

I use echo all the time, the only thing I use really - I think the print command is there for those used to Perl, to make the switchover to PHP.

Actually you will hear arguments about performance issues between them, but I use printf most of the time because it alleviates the need to escape quotes and concatenate variables. But here are the main differences

$name = “Joe”;
print “Hello $name”;

and echo “Hello $name”;

and printf(‘Hello %s’, $name);

will all output Hello Joe

printf() some may say will take more time to process, but in my mind I don’t have any sites that are getting SO MUCH traffic that it would make a visible difference.

Consider this would you rather write

$var = “<tr><td width=\“150\”>Hello " . $name . " How are You? Today is “. DATE(“Y-m-d”) .”</td></tr>”;

or

$var = sprintf(‘<tr><td width=“150”>Hello %s How are You? Today is %s</td></tr>’, $name, DATE(“Y-m-d”));

To me the sprintf() option is a lot easier to code and debug

I didn’t know that - thanks for the info! I’m already deep in the habit of using echo() though, so I probably won’t be able to break it. More work - but it builds character, or something like that. :smiley:

Still by far the fastest way to output in PHP is to only echo/print info that needs to be printed:

e.g.

<table>
<tr>
<td>
<?php echo $cellInfo; ?>
</td>
</tr>
</table>

printf() wasn’t designed for just printing out varibale like

print $var; as in Karl’s example.

Where it really helps is for orgainzing output, while alleviating the need to concatenate variables or escaping quotes.