Since print and echo statements do virtually the same thing, there has been a lot of debate on which is better to use. While struggling thru chapter 7, I came up with a good time saving technique that saves some programming time. I say, USE BOTH, but for different reasons. Print for temporary debugging output, and echo for outputting HTML that you want to stay permanently in the page when your done.
In the check_image.php file that appears in chapter 7 and is modified 4 or 5 times throughout. I took the approach of laying out the basic structure of the code like this:
Code:
if ($POST['submit'] == "Upload" {
print "User just uploaded</br>";
// upload code goes here
....
echo "<h1>How does it feel to be famous?</h1>";
....
} else {
if ($POST['submit'] == "Save") {
print "User pressed Save</br>";
// Save code goes here
echo "<p>Some HTML</p>";
} else {
print "User pressed Preview</br>";
print $some_variable . "</br>";
echo <img src="...........:
}
print "<div>";
print "<pre>"; echo '$_POST '; echo print_r($_POST); echo "</pre>";
print "<pre>"; echo '$_GET '; echo print_r($_GET); echo "</pre>";
print "<pre>"; echo '$_FILES '; echo print_r($_FILES); echo "</pre>";
print "</div>";
I do it this way so I can work on different parts of the program and get one thing working and not get parsing errors cause something above isn't working just yet. ( That last div is my way of keeping track of what's being pass to and from my program - a typical source of problems.)
OK. Now to the point. Echo VS. Print. At the end when you've got that 300 plus line piece of code working, you've got to go back thru and comment out all the debugging code that produces output that you don't want your users to see. And you're probably pretty tired by this point. Sorting thru all that code to get rid of the little outputs that make your page look unprofessional can be quite a chore.
BUT, if you have used the convention of print for debugging and echo for permanent html, all you need to do is use your editor's find function to search for "print". It will take you ( or "next" you) to the exact lines you need to comment out ( all the "print" and the "print_r" statements). Easy.
If you've used echo throughout, you need to look hard at the code you find to make sure if it's supposed to be there or not. Not easy, especially when your tired, from having wirtten your program. I don't know how many times I've broken my code during that clean up process. Hopefully this technique will help save you from that last minute mistake that can add hours to fixing code and that "BUT IT WORKED just a few minutes ago!" frustration.
HTH
Boz