Wrox Programmer Forums
|
BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143
This is the forum to discuss the Wrox book Beginning PHP 6, Apache, MySQL 6 Web Development by Timothy Boronczyk, Elizabeth Naramore, Jason Gerner, Yann Le Scouarnec, Jeremy Stolz; ISBN: 9780470391143
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP 6, Apache, MySQL 6 Web Development ISBN: 9780470391143 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 21st, 2010, 01:31 PM
Authorized User
 
Join Date: Jan 2010
Posts: 25
Thanks: 1
Thanked 4 Times in 4 Posts
Lightbulb Tip: print vs. echo debate

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





Similar Threads
Thread Thread Starter Forum Replies Last Post
I need a echo wizard Anne1 PHP How-To 1 December 16th, 2006 04:20 AM
echo "\n" problem jun99 PHP How-To 4 November 29th, 2005 01:01 PM
echo "\n" problem jun99 Beginning PHP 0 November 27th, 2005 05:12 PM
Echo / print variable without html code Mantis Pro PHP 4 April 1st, 2005 01:28 PM
using echo command to display "$" Tachyon PHP How-To 3 March 1st, 2004 09:54 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.