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?
1
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.
10
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.
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.