I have a few pages in my site that echo out an HTML page once a PHP function has been executed or not (a success page or an error page). However i have noticed that over time i have done this 3 different ways on different pages. Can someone please advise me on what is the correct way to carry out this task, thanks 1. - the echo HTML has been carried out within the PHP tags and each html line starts with ' and ends in '."\n\n". <?php // php code // php code // php code // all this has been successful echo('<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"'."\n". '"http://www.w3.org/TR/html4/strict.dtd">'."\n\n". '<html lang="en">'."\n". ' <head>'."\n". ' <meta http-equiv="content-type" content="text/html; charset=iso-8859-1">'."\n\n". ' <meta http-equiv="imagetoolbar" content="no">'."\n\n". ' <meta http-equiv="refresh" content="4;URL=contact.php">'."\n\n". ' <link rel="stylesheet" type="text/css" href="stylesheet.css">'."\n\n". ' <link rel="shortcut icon" href="images/favicon.ico" >'."\n\n". ' <title>Successful</title>'."\n\n". ' </head>'."\n\n". ' <body> <div align="center"><img src="images/headers/correct.jpg"> </div>'."\n\n". ' <div align="center"><img src="images/oops.gif"> </div>'."\n\n". ' <div align="center" id="Upload">'."\n\n". ' <p class="p">Well done</p>'."\n\n". ' <p class="p">Please wait for page to reload . . . </p>'."\n\n". ' </div></body>'."\n\n". '</html>'); ?> PHP: 2. - the echo HTML has been carried out within the PHP tags but each HTML line doesn't start with ' and doesn't end with '."\n\n". <?php // php code // php code // php code // all this has been successful echo ("<!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN> <html> <head> <meta http-equiv=content-type content=text/html; charset=iso-8859-1> <meta http-equiv=imagetoolbar content=no> <meta http-equiv=refresh content=2;URL=my_account.php> <link rel=stylesheet type=text/css href=stylesheet.css> <link rel=shortcut icon href=images/favicon.ico > <title>Successful</title> </head> <body> <div align=center><img src=images/headers/correct.jpg> </div> <div align=center><img src=images/oops.gif> </div> <div align=center> <p class=upload_error>Well done</p> <p class=p>Please wait for page to reload ... </p> </div> </body> </html>"); ?> PHP: 3. - the echo HTML has been carried out outside the PHP tags <?php // php code // php code // php code // all this has been successful ?> PHP: <!DOCTYPE HTML PUBLIC -//W3C//DTD HTML 4.01 Transitional//EN> <html> <head> <meta http-equiv=content-type content=text/html; charset=iso-8859-1> <meta http-equiv=imagetoolbar content=no> <meta http-equiv=refresh content=2;URL=my_account.php> <link rel=stylesheet type=text/css href=stylesheet.css> <link rel=shortcut icon href=images/favicon.ico > <title>Successful</title> </head> <body> <div align=center><img src=images/headers/correct.jpg> </div> <div align=center><img src=images/oops.gif> </div> <div align=center> <p class=upload_error>Well done</p> <p class=p>Please wait for page to reload ... </p> </div> </body> </html> HTML: WHAT IS THE DIFFERENCE IN THESE THREE - SORRY I'M NOT SURE WHERE OR HOW I ENDED UP USING 3 DIFFERENT METHODS - THANKS FOR YOUR HELP
Ok, the difference between methods 1 and 2 is very slight. The difference would mainly become apparent if one were to do a "view source" on the served pages. In method 1, the html code would appear pretty much as you'd written it. With method 2, it would all appear on 1 line (as you'd not inserted any line breaks). The html would, of course, render ok - as the line breaks are ignored when rendering. The advantage of method 2, therefore, would be a slightly quicker transmission to the client as you're sending a few bytes less. (31 bytes less by my count). Method 3 doesn't use php to print the output. Which clearly is going to be quicker as the server isn't doing anything. As for which you'd use - it depends on what you're doing. If I have large chunks of pure static HTML, I tend to close the php tags, and just keep the raw html. If you have a lot of dynamic stuff to insert, it's often easier to echo the whole lot. So, horses for courses really?
Also, there is difference between method 1 and 2..... 1 uses " " around attributes and 2 doesn't - WHAT IS THE CORRECT PROCEDURE HERE
An echo takes microseconds to execute. Strings (text in layman's terms) in single quotes executes marginally faster. Double quotes allows you to intersperse text with variables and escape characters, such as \n (represents a new line). The difference is barely noticeable, especially if strings in double quotes are written properly ($ signs escaped etc.).
Yes, true but PHP is an interpretted language and all those micro seconds add up. And if that page is getting hit millions of times a day its eventually gonna become an issue. And it wasn't just a single echo, was it? Method 1 contains: 1 echo 34 concatenations about 70 substitutions (parsing for the /n and substituting it for a chr 13) That's without all the syntax checking the interpretters got to do So, it could conceivably more than double the execution time of the page. If all your pages take twice the time to serve as they need to, that means you may eventually need a server twice as powerful as you could have had! Or, to look at it another way, when you hit page 1 of digg, your site falls over twice as quickly under the strain. And if that wasn't enough, if your pages take twice as long to serve, then that's a bigger proportion of people who close the window before they ever get to see/click your adverts. So it directly costs you money. Why echo stuff you don't need to? Why make PHP syntax check stuff it doesn't need to? Why add overhead to your server you don't need to? Where's the advantage in echoing just for the sake of it?
The " " are required otherwise it's not valid HTML. If you were to do that using method 2 you'd have to use echo "<body> <div align=\"center\"><img src=\"images/headers/correct.jpg\"> </div>"; PHP: I'm with ecentricNick on this one - for big chunks, end the php and stick the raw html in - on top of all the speed advantages, it's easier to read too. P.S. Echo isn't a function so you don't need parentheses.