Ok before starting off I have to admit that I am a casual web programmer. Read casual as NO EXPERT.
Secondly this writing has been triggered by PHP.
I have been involved in maintaining a Joomla project. Adding a few features here and there and correcting bugs introduced by the original team.
In my experience standard php programming can look like any of the following:
- foo.html
<html> <head>
…
with php code interspersed anywhere in the code like below:
<?php echo 'I am php code';?>
OR like
- gee.php
<?php echo '<html><head></head><body><h1>'; echo 'Html code is prepared using echo or print statements'; /* 'But the source everywhere is basically php and html strings printed out' */ ?>
I think you get the idea. Your file’s main language is either HTML and when needed php code segments are used, or your file starts with <?php and ends in ?>. In the second case html code is prepared using strings of various sorts.
Logic dictates, as Doctor Spock would put it, that when there is a need for complicated php code with lots of conditionals, loops and so on it’s better to separate the php and the html code by either:
a) Using include statements where you include the piece of code that ideally can be reused elsewhere and at the same time obstruct it from your current code view.
For example:
<?php // Beginning of HTML file DOCTYPE include("html_preamble.php"); //<head> section ... include("main_header.php"); echo '<div id="maincontent">'; ...
b) Using some additional templating engine like smarty etc.
My, admittedly more than often flawed, logic absolutely prohibits me from understanding this piece of code from a custom Joomla template:
<a <?php if ($shopperTask=='recommendations') echo 'class="active"'; ?>href="index.php?option=
com_virtuemart&page=shop.product_details&flypage=flypage-ask.tpl&Itemid=<?=$Itemid?>
&category_id=<?=$category_id?>&shopperTask=recommendations&product_id=<?=$product_id?>
"><?php echo JText::_('Recommendations'); ?></a>
Here I can count at least 5 (FIVE) php sections in a simple <a href=””></a> html tag. Why the particular author decided to open and close five times PHP sections instead of preparing the html string in advance eludes me.
However I have been noticing this kind of reckless, ad-hoc way of writing php code more and more often. In fact I have seen this kind of code Jambalaya too many times involving some web scripting language, SQL, HTML, Javascript, AJAX, XML without any obvious effort to separate things.
I am not suggesting that this problem can be avoided completely. However with little effort the tools that the language offers can be used to separate or homogenize the different elements in a sane way.
In PHP, in particular, there are a few weapons that can be used:
- The include statement, as mentioned above;
- HERE DOCUMENTS
Here documents, known in the UNIX world for decades, are surprisingly underused when they can be so useful.
Using HERE DOCUMENTS you can define multiline strings while php variables can still be interpolated. The syntax looks like below:
<?php $number = 5; $myvariable = <<<customarker blah blah this can be any string <html> including single ' double " quotes and it will properly interpolate your variables like number = {$number} customarker; ?>
The output is:
blah blah this can be any string <html> including single ' double " quotes and it will properly interpolate your variables like number = 5
The only thing to remember is that the end marker (in this case customarker) MUST be in a line of itself without any other characters (e.g. whitespaces) apart from the semicolon after it.
Nice post u have here 😀 Added to my RSS reader
There is always the , which prints out directly the variable $foo without the ‘echo’ command.
This abbreviation makes the code a little more easy to read and maintain.