Which way is proper PHP?

LotsOfZeros

^^^ Bi-Winning ^^^
Feb 9, 2008
4,648
118
0
www.makemoniesonline.com
PHP embedded within HTML:
Code:
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        <?php
        // put your code here
        ?>
    </body>
</html>
Or, echo/print out the HTML output:
PHP:
<?php
echo "<html>\r\n<head>\r\n<title>Title</title>\r\n</head>\r\n<body>\r\n";
// put your code here
echo "</body>\r\n</html>";
?>
 


Either way works as long as the server, serving the page recognizes its a php file or it have a .php extention.

You can actually mix and match both methods if you chose.

I prefer the first method personally.

EDIT: php tags can be put anywhere on the page too, not just inside the <body> tag.
 
Most of the time, I'll use whichever way the programmer before me used.


Otherwise, because I convert a lot of static html themes to work with CMS's, I'll use html tags for the page elements that mostly stay static like header, footer and php echo for sidebars, dynamic menus, etc.
 
Either, but less fucking about to do it the first way. I also just assumed that it took more server processing to do it the second way = slower loading pages (even if it is only very slight).
 
I have been doing it both ways for years. One day I got curious as to which way was better from a standards issue and from a performance issue.
The second way is a bit more painful for me since I have to escape so much of the html so if it's all the same, I'm switching to #1 or a mixture of the two.
 
I use to do basically number 2 when starting out, believe me #1 seems like it would be harder but I find it easier as I'm writing native html and just populating it with php, it's simpler from that aspect.
 
making sure you open with <?php is proper (since short tags won't be supported past 5.3 anymore), but some say that opening and closing the tags in the parser as opposed to just echoing everything out can take up more resources. If it does it would be rather minimal. I usually do a mix of the two where it seems appropriate.

In the long run, the first way will be a lot easier to debug, my general rule of thumb is to first process data, then interpret the output as opposed to mixing it too much. (that way you normally end up with a php block at top to process and stash the data, and the rest are smaller outputs).
 
MVC is the most retarded shit ever, period.
Yeah, I hate MVC. If you plan from the beginning, you don't need it, unless you have 10 developers from India working on one huge-ass site.

This is approximately how a lot of my sites start:
index.php said:
<?php

require_once('header.php');
require_once('navigation.php');
require_once('body.php');
require_once('footer.php');

?>

You want to encapsulate everything in PHP in case you need to send some meta shit first like redirects, or if it's a membership site so you can load shit based on the membership level of the viewer.
 
Depends on the app - you should use what's most fitting in the circumstances.

Also ease of debugging is a big factor as kblessinggr says.

For apps with a lot of html you should really be templating as it keeps all the processing clear from the templates and is MUCH easier to maintain in the long run.

If the script is a short and sweet few lines (less than 50) I just bang it out however - usually all in php as I find it easier to see what's going on the embedded <?php tags.

Anything more then plan it properly and template it wherever possible.
 
PHP embedded within HTML:

PHP:
<?php
echo "<html>\r\n<head>\r\n<title>Title</title>\r\n</head>\r\n<body>\r\n";
// put your code here
echo "</body>\r\n</html>";
?>

As for the argument that it's a pain to escape stuff, you don't really need all the new lines in there unless you want to have a pretty HTML output... Otherwise you could just leave them out.

I do prefer your first method too whenever I have the choice though.
 
for small stuff use the first method, the second becomes totally unmaintainable after few edits

that said, I've used and loved smarty: at the moment I'm working mostly on WP based stuff, however the next time I'll build something from scratch I'll use smarty again
 
for small stuff use the first method, the second becomes totally unmaintainable after few edits

that said, I've used and loved smarty: at the moment I'm working mostly on WP based stuff, however the next time I'll build something from scratch I'll use smarty again

I agree. I have used Smarty on a few projects and liked keeping them separate only problem is Smarty is slow when gaining a lot of traffic.

Smarty 3 is coming out and they claim it will be faster but I'm sure the debate of using a template engine or not will continue to rage on.
 
In the long run, the first way will be a lot easier to debug, my general rule of thumb is to first process data, then interpret the output as opposed to mixing it too much. (that way you normally end up with a php block at top to process and stash the data, and the rest are smaller outputs).


When not using a framework that's usually what I do. The code at the top handles the request(or sends it off to be handled), then I save relevant data to a $template variable and then include/require a php file that would look something like this:

Code:
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title></title>
    </head>
    <body>
        Hi, <?php echo $template['username']; ?>.
    </body>
</html>
It stays simple and achieves the same thing that MVC aims to achieve, which is keeping business logic separate from presentation.

As far as using smarty:

 
I clearly vote for version #1

MVC is only one design pattern - using it for large systems mostly (scalability) because it slows down processing.

Lightweight php-frameworks like "recess" (Recess PHP Framework) are doing a very good job in terms of performance (compared to Zend FW and the likes).
Additionally I am using opcode caching wherever possible - makes smarty dispensable...somehow
 
Which do you have more of - PHP or HTML? Make that the determining factor. If everything will be dynamic and needs conditional displaying create PHP that writes out HTML. If you only need a little work in PHP do it the other way.