1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

echo HTML

Discussion in 'PHP' started by oo7ml, Aug 14, 2007.

  1. #1
    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
     
    oo7ml, Aug 14, 2007 IP
  2. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #2
    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?
     
    ecentricNick, Aug 14, 2007 IP
  3. oo7ml

    oo7ml Well-Known Member

    Messages:
    656
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    105
    #3
    Also, there is difference between method 1 and 2..... 1 uses " " around attributes and 2 doesn't - WHAT IS THE CORRECT PROCEDURE HERE
     
    oo7ml, Aug 14, 2007 IP
  4. krt

    krt Well-Known Member

    Messages:
    829
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    120
    #4
    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.).
     
    krt, Aug 14, 2007 IP
  5. ecentricNick

    ecentricNick Peon

    Messages:
    351
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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?
     
    ecentricNick, Aug 14, 2007 IP
    void likes this.
  6. void

    void Peon

    Messages:
    119
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    0
    #6
    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.
     
    void, Aug 14, 2007 IP