phppup Posted March 20, 2012 Share Posted March 20, 2012 I understand that I can place my HTML code outisde of my PHP tags and have it publish, OR i can put it inside the PHP tags and ECHO every line. Am I correct in thinking that either will provide the same result? Is there an advantage to one method over the other?? Quote Link to comment https://forums.phpfreaks.com/topic/259341-html-versus-php/ Share on other sites More sharing options...
dragon_sa Posted March 20, 2012 Share Posted March 20, 2012 Depends on what you are doing, if there is lots of php results or variables in your html sometimes it easier to echo it out, if you have lots of processing or functions I like to put the output and html into a variable in the top of a page and then echo it out where I need it on the page later, so the layout doesnt get cluttered with php script. You want to make the code easy to read and non repetitive and separate the layout from the scripts. Quote Link to comment https://forums.phpfreaks.com/topic/259341-html-versus-php/#findComment-1329469 Share on other sites More sharing options...
Nodral Posted March 20, 2012 Share Posted March 20, 2012 I personally find it better to have all my HTML in a seperate file. Then create variables loaded with the dynamic content and include the output file. eg //HTML PAGE called output.php <body> <div> <h1> <!--etc etc --> <?php echo $output; ?> </h1> </div> </body> //php $output ="Hello World"; include("output.php") Everything is much tidier, and if you're clever about it you can use the same output.php page for practically everything. I would then have a seperate file for CSS and anothe rfor Javascript which would also be called by the output.php Quote Link to comment https://forums.phpfreaks.com/topic/259341-html-versus-php/#findComment-1329471 Share on other sites More sharing options...
seanlim Posted March 20, 2012 Share Posted March 20, 2012 They are effectively the same. However, as mentioned above, you will want to separate the logic from the presentation as much as possible to make your code readable and easy for maintenance and debugging. Take a look at the some templating options or even the MVC architecture. So, to answer your question they will provide the same results. However, if you are just printing plain HTML, it might be *slightly* faster if you do it outside of the <?php ?> tags (compared to using echo "...") as it won't have have to run through the PHP parser. Quote Link to comment https://forums.phpfreaks.com/topic/259341-html-versus-php/#findComment-1329474 Share on other sites More sharing options...
jGiblets Posted March 20, 2012 Share Posted March 20, 2012 I personally find it better to have all my HTML in a seperate file. Then create variables loaded with the dynamic content and include the output file. I like this idea, but is there a limit, or tradeoff, when it could slow things down? What if the OP's site had 6 pages and each page pulled 12 HTML content elements via PHP variables (or include/require) and each page had 3-5 style sheets attached. Would visitors experience any delay in pulling all that content vs HTML + PHP on the page? Related, is there a reason to choose echoing $variables instead of using include()/require()? Quote Link to comment https://forums.phpfreaks.com/topic/259341-html-versus-php/#findComment-1329503 Share on other sites More sharing options...
Nodral Posted March 20, 2012 Share Posted March 20, 2012 As we're into the 'Green' era of recycling, I've used this code on practically every site I've worked on to display my HTML <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE><?php echo $title; ?></TITLE> <META NAME="Generator" CONTENT="*******"> <META NAME="Author" CONTENT="**********"> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> <?php echo $redirect; ?> <script type="text/javascript" src="myScripts.js"></script> <!-- a few page styles--> <link rel="mystyles.css" type = css/text> </HEAD> <BODY> <div id="wrapper"> <div id="banner"> <?php echo $banner; ?> </div> < <div id="content"> <?php echo $output; ?> </div> </div> </BODY> </HTML> As you can see, I can simply do my processing and then include this file with all / some of the variables set. I've got another couple I include if I need a 2 column or 3 column display Quote Link to comment https://forums.phpfreaks.com/topic/259341-html-versus-php/#findComment-1329507 Share on other sites More sharing options...
Muddy_Funster Posted March 20, 2012 Share Posted March 20, 2012 I personally find it better to have all my HTML in a seperate file. Then create variables loaded with the dynamic content and include the output file. I like this idea, but is there a limit, or tradeoff, when it could slow things down? What if the OP's site had 6 pages and each page pulled 12 HTML content elements via PHP variables (or include/require) and each page had 3-5 style sheets attached. Would visitors experience any delay in pulling all that content vs HTML + PHP on the page? Related, is there a reason to choose echoing $variables instead of using include()/require()? look at it this way, if you have everything on the one page, all html, php and even some css and a bit of javascript, using variables and code blocks - how much more of a pain in the ass is it going to be to fix or update a couple of lines of the code (or a couple of div tags, or the onClick event of a perticular button) when you have to sift through several hundred, up to a couple of thousand, lines of everything to get to it? then you miss off one ; or a " and nothing on your page works or displays properly at all. It's just common sense to keep them, and treat them, as seperate entities (that's my tuppence worth anyway) Quote Link to comment https://forums.phpfreaks.com/topic/259341-html-versus-php/#findComment-1329510 Share on other sites More sharing options...
Mahngiel Posted March 20, 2012 Share Posted March 20, 2012 I agree with muddy, here. Separation of you logic and display code is paramount. There are ways many newcomers to PHP don't realize is how to use short tags. Take this into account: <?php $a = array('1', '2', '3', '4'); if($a) { echo '<ul>'; foreach($a as $b) { echo '<li>' . $b . '</li>'; } echo '</ul>'; } ?> Seems sort of retarded and cumbersome to do that. Instead, I would do: // This really comes from your logic, but shown for purpose <?php $a = array('1', '2', '3', '4'); ?> <?php if($a): ?> <ul> <?php foreach($a as $b): ?> <li><?php echo $b; ?></li> <?php endforeach; ?> </ul> <?php endif; ?> Of course, this is not as verbose and deep as it normally is, but you can see the benefit of keeping that stuff out of your PHP, because that HTML code really has no business there. Quote Link to comment https://forums.phpfreaks.com/topic/259341-html-versus-php/#findComment-1329523 Share on other sites More sharing options...
KevinM1 Posted March 20, 2012 Share Posted March 20, 2012 I personally find it better to have all my HTML in a seperate file. Then create variables loaded with the dynamic content and include the output file. I like this idea, but is there a limit, or tradeoff, when it could slow things down? What if the OP's site had 6 pages and each page pulled 12 HTML content elements via PHP variables (or include/require) and each page had 3-5 style sheets attached. Would visitors experience any delay in pulling all that content vs HTML + PHP on the page? The cost is negligible. Application bottlenecks tend to happen with the db, either with poorly designed tables, or poorly designed queries (especially with queries executed in loops). Include/require is essentially cut + paste. They literally paste the content of the particular file, character-by-character, at the spot where they were invoked. Since this all happens in memory, likely all that's going on is an internal pointer moving from file to file. In short, it's nothing to worry about. The benefits of separating presentation from logic far outweighs any performance hit (which would be in the milliseconds). Related, is there a reason to choose echoing $variables instead of using include()/require()? There's two things going on in the code above. Two files. Template (mostly HTML, with just enough PHP to display specific data): <body> <div> <h1> <!--etc etc --> <?php echo $output; ?> </h1> </div> </body> The main script, which then uses the template: $output ="Hello World"; include("output.php") In real life, the template would likely be in a folder with other templates. The main script(s) would then include/require whichever one was needed after doing all the heavy lifting of processing forms, querying the db, etc. Quote Link to comment https://forums.phpfreaks.com/topic/259341-html-versus-php/#findComment-1329567 Share on other sites More sharing options...
Psycho Posted March 20, 2012 Share Posted March 20, 2012 I agree with muddy, here. Separation of you logic and display code is paramount. There are ways many newcomers to PHP don't realize is how to use short tags. Take this into account: <?php $a = array('1', '2', '3', '4'); if($a) { echo '<ul>'; foreach($a as $b) { echo '<li>' . $b . '</li>'; } echo '</ul>'; } ?> Seems sort of retarded and cumbersome to do that. Instead, I would do: // This really comes from your logic, but shown for purpose <?php $a = array('1', '2', '3', '4'); ?> <?php if($a): ?> <ul> <?php foreach($a as $b): ?> <li><?php echo $b; ?></li> <?php endforeach; ?> </ul> <?php endif; ?> Of course, this is not as verbose and deep as it normally is, but you can see the benefit of keeping that stuff out of your PHP, because that HTML code really has no business there. Really? I absolutely abhor the use of PHP opening/closing tags on the same line repeated ad nausea. My preferred approach to the above would be to use double-quoted strings so I don't have to enter/exit the quotes to use a variable. Plus, it has the added benefit of being able to easily add some "formatting" to the output such as linebreaks and tabs so the HTML is easily readable. Amazes me how many people output the HTML content all in one line. Absolutely a nightmare to debug an HTML problem. $a = array('1', '2', '3', '4'); if($a) { echo "<ul>\n"; foreach($a as $b) { echo "<li>{$b}</li>\n"; } echo "</ul>\n"; } Or as, Nodral stated, put the content into a variable to output within a template/output file Quote Link to comment https://forums.phpfreaks.com/topic/259341-html-versus-php/#findComment-1329596 Share on other sites More sharing options...
phppup Posted March 20, 2012 Author Share Posted March 20, 2012 I think i got way more than i bargained for with this one! LOL I'd better MARK it SOLVED before the gloves come out! Quote Link to comment https://forums.phpfreaks.com/topic/259341-html-versus-php/#findComment-1329609 Share on other sites More sharing options...
Mahngiel Posted March 20, 2012 Share Posted March 20, 2012 Really? I absolutely abhor the use of PHP opening/closing tags on the same line repeated ad nausea. Or as, Nodral stated, put the content into a variable to output within a template/output file My point was not for such simple formatting as a one level list, but for more complex code when you are entering in and out of HTML elements and PHP values. But, alas, everybody has their own methodology, and I find no problem in that! Quote Link to comment https://forums.phpfreaks.com/topic/259341-html-versus-php/#findComment-1329610 Share on other sites More sharing options...
Andy-H Posted March 20, 2012 Share Posted March 20, 2012 As we're into the 'Green' era of recycling, I've used this code on practically every site I've worked on to display my HTML <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <HEAD> <TITLE><?php echo $title; ?></TITLE> <META NAME="Generator" CONTENT="*******"> <META NAME="Author" CONTENT="**********"> <META NAME="Keywords" CONTENT=""> <META NAME="Description" CONTENT=""> <?php echo $redirect; ?> <script type="text/javascript" src="myScripts.js"></script> <!-- a few page styles--> <link rel="mystyles.css" type = css/text> </HEAD> <BODY> <div id="wrapper"> <div id="banner"> <?php echo $banner; ?> <> < <div id="content"> <?php echo $output; ?> <> <> </BODY> </HTML> As you can see, I can simply do my processing and then include this file with all / some of the variables set. I've got another couple I include if I need a 2 column or 3 column display Seriously? 4.0 transitional and block-caps tags and attributes? /?EDIT To expand, if you're looking to promote re-usable future-proof code, look into HTML 4.01 strict (/best practices) Some of this includes a space before the closing > of an un-closed tag: <!-- META //--> <meta name="description" content="Secure login" > <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" > <!-- LINKS AND SCRIPTS //--> <link rel="stylesheet" href="css/login.css" type="text/css" > this way, if you ever need to switch to XHTML, search and replace " >", " />" Also, HTML 4.01 strict is the closest version to HTML 5, so is likely to most resemble it, using this with best practices should make the switch to HTML 5 a little easier. Quote Link to comment https://forums.phpfreaks.com/topic/259341-html-versus-php/#findComment-1329620 Share on other sites More sharing options...
phppup Posted March 20, 2012 Author Share Posted March 20, 2012 This has to have been one of the MOST educating of 'simple questions' that I've ever asked in the forum. Good exchanges and loads of good info. I personally use HTML strict figuring I'd like to KNOW it's correct than go with the browsers assumptions. I'll leave this one open til tomorrow, in case anybody else wants to chime in. (not sure, can it still be posted into after it's marked solved?) Quote Link to comment https://forums.phpfreaks.com/topic/259341-html-versus-php/#findComment-1329640 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.