Jump to content

coding with HTML and PHP together


cbassett03

Recommended Posts

Which methods is generally accepted as the standard when putting PHP and HTML in the same file?

 

<?php

  echo "<html>\n";
  echo "<head>\n";

  echo "<body>\n";
  echo "<p>Hello, you are ".$age." year(s) old!</p>\n";
  echo "</body>\n";
  echo "</hjtml>\n";
?>

 

...versus...

 

<html>
<head>
<body>

<?php
echo "<p>Hello, You are ".$age." year(s) old!</p>\n";
?>

</body>
</html>

 

Is it preferred to have the script ECHO the HTML tags to the page, or should I just embedd them (the tags that do not require any PHP manipulation, such as the HTML, HEAD, BODY, etc.  Is one faster than the other?  Which is more secure?

 

I know with the first example, you would have to escape any quaotation marks, which makes it a bit trickier to do with certain HTML, but I'm more concerned about standards and security than coding complexity.  My previous programming experience tells me that the second would be faster, since PHP simply spits out the extra stuff (the HTML, HEAD, BODY tags) rather than using ECHO to send them to the pa ge, which thus reduces processing time, right?

Link to comment
https://forums.phpfreaks.com/topic/213713-coding-with-html-and-php-together/
Share on other sites

Which methods is generally accepted as the standard when putting PHP and HTML in the same file?

 

<?php

  echo "<html>\n";
  echo "<head>\n";

  echo "<body>\n";
  echo "<p>Hello, you are ".$age." year(s) old!</p>\n";
  echo "</body>\n";
  echo "</hjtml>\n";
?>

 

...versus...

 

<html>
<head>
<body>

<?php
echo "<p>Hello, You are ".$age." year(s) old!</p>\n";
?>

</body>
</html>

 

Is it preferred to have the script ECHO the HTML tags to the page, or should I just embedd them (the tags that do not require any PHP manipulation, such as the HTML, HEAD, BODY, etc.  Is one faster than the other?  Which is more secure?

 

I know with the first example, you would have to escape any quaotation marks, which makes it a bit trickier to do with certain HTML, but I'm more concerned about standards and security than coding complexity.  My previous programming experience tells me that the second would be faster, since PHP simply spits out the extra stuff (the HTML, HEAD, BODY tags) rather than using ECHO to send them to the pa ge, which thus reduces processing time, right?

 

HTML does not require security as far as I know, considering it's just a skeleton/design of your webpage.

If you're going for the first option, it's fine to have multiple echos like that but you might want to look at the heredoc syntax.

 

echo <<<html
<html>
   <head>
      <title></title>
   </head>
   <body>
   </body>
</html>
html;

Just remember that there can't be any whitespace before the ending of the heredoc syntax, so this is invalid:

echo <<<html
This is some output.
   html;

 

Another thing to note is that if you are using something like this you may encounter header errors when attempting to set cookies or redirect the page because output has already been sent to the browser.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.