Jump to content

HTML versus PHP


phppup

Recommended Posts

 

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??

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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()?

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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)
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

 

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!  ;D

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?)

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.