Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

I'm a self taught PHP programmer. Because I have no formal training in PHP, I often find my coding style to be considerably different than example code I find on the Internet. I've slowly started adapting some of the more common practices I find. However, before changing my habits, I try to find a good reason to use the new style. I just don't see much reason to change my habit if it isn't actually advantageous.

I see a lot of people write PHP code such as :

<h1><?php echo $cTitle; ?></h1>
<div><?php print $cContent; ?></div>

Whereas I generally write :

<?php
    echo "<h1>", $cTitle, "</h1>";
    print("<div>" . $cContent . "</div>");

What I'm seeing is that people are commonly going in-and-out of PHP, while I'll just use PHP to print/echo the html content along with the PHP variables.

Is there a measurable difference between these two methods? Is there a good reason for me to change my ways?

share|improve this question

closed as primarily opinion-based by Bart van Ingen Schenau, tylerl, gnat, World Engineer Apr 8 '14 at 0:17

Many good questions generate some degree of opinion based on expert experience, but answers to this question will tend to be almost entirely based on opinions, rather than facts, references, or specific expertise. If this question can be reworded to fit the rules in the help center, please edit the question.

3 Answers 3

up vote 4 down vote accepted

I am no fan at all of embedding PHP inside HTML (like your first example). That just makes a huge mess of things and makes it difficult to separate the code from display. Some systems are set up to use PHP as a template engine so there's no avoiding it inside of views. But in systems I build from scratch, I never do the former, and don't do a lot of the latter of your examples, preferring to use templates.

So basically, no, don't go a'changing. Remember, a large chunk of the PHP code you see online either comes from really old tutorials back from the days where one php file = one page, or from people who learned PHP from said tutorials.

share|improve this answer
    
Thanks. My big worry about switching to that method was the mess it seems to cause in the code (especially with things like ifs/loops). Nice to know someone else out there stays away from it as well. –  DragonYen Apr 3 '14 at 17:35
2  
I don't think, templating engines are any good from a programmers point of view. It adds overhead to the system, cannot do anything, php cannot do, and it is sort of a new language you ( and the designer ) has to learn. And you can of course device business logic from presentation logic ( cf. MVC ). From a security point of view a templating engine might be a good idea though. But look at drupal. It uses php as templating engine by default. –  HerrSerker Apr 3 '14 at 20:28
    
My primary job at work is to maintain our Drupal website. So a lot of the extraneous HTML is already handled for me. However, I'm also tasked with quite a bit of custom code that we couldn't find good community modules for. So I still find myself outputting my fair share of h, div, ul, table, etc tags –  DragonYen Apr 3 '14 at 20:39
    
@HerrSerker and I think intermingling PHP with HTML isnt any good from a programmers stand point. I don't like application logic intermingled with the display. I keep code as code, and markup as markup. –  GrandmasterB Apr 4 '14 at 3:29
1  
@HerrSerker: A templating engine that can do everything PHP can do is just as bad as PHP. Templating engines in almost every other language focus on providing a limited set of actions that prevent the heavy coupling of code and layout you get if you're placing anything non-trivial in a template. You're looking at it the wrong way--you don't use a templating engine for power, you use it for constraint. –  Phoshi Apr 4 '14 at 10:58

The reason you see a lot of html templating is probably because of an "appearence driven" workflow.

I.E. You start by coding up an HTML page that "looks right" then add in the dynamic php elements later.

This works well if you also follow a classic Model View Controller pattern -- essentially you do all your business logic in php before invoking the template which should have minimal php code consisting mostly of simple substitution of previously calculated values.

share|improve this answer

Template are always shitty in the end, they are all HTML which is being modified to function in the system:

<h1><?php echo $cTitle; ?></h1>
echo "<h1>", $cTitle, "</h1>";
print("<div>" . $cContent . "</div>");
<h1>{{cTitle}}</h1>
<h1 data-content="cTitle"></h1>

etcetera etcetera.

In the end something has to be identified as the place where you should put content in. Nothing to be done about it. So what is better? None of them, it's a matter of details and a matter of taste and depends on your project. I think templates are one of the oldest active discussions among php developers with no answer to it.

There is another reason why there is no solution, it's: separation of concerns. The view (if you use MVC or something like that) is the place where you define which content gets somewhere. That is because for example the controller does not know that. There is also a reason for that: The controller has a choice, you give a simple example now, but in the real world there might be a HTML and a XML view:

//HTML:
<h1><?=$cTitle;?></h1>

//XML:
<xml>
  <title><?=$cTitle;?></title>
</xml>

So, you have to do it somewhere. No real other options are available. If you really don't want the templates with tags you could only split out the view layer in 2 separate layers which could give for example:

//template (a new layer below the view)
<h1>This is a demo title</h1>

//view code
$this->h1($cTitle);

Then you could use something like a jQuery style to find the right tags to assign content to. That way you can use a static HTML design and just fill in the gaps though it will still break when someone changes the format of the template.

So, in the end, yes we still need some kind of tags inside the templates to get things really functional.

Not very relevant as answer but informative: When you work with a lot of developers there are always preferences on templating. In the end we decided to go for PHP in the templates with the developer letting choose their preferred way. Whether they do echo or embed the php tags doesn't make a lot of difference. Anybody with good PHP knowledge can read both. So there is a bit of freedom but it's clear which options to choose from. In the end it does not make a lot of difference with experienced developers as long as work is being done well and structured.

share|improve this answer

Not the answer you're looking for? Browse other questions tagged or ask your own question.