Switching between HTML and PHP

For a long time there were multiple ways to switch between HTML and PHP in your server side code. The removal of some of these ways of starting a PHP block from HTML in PHP 7 goes a long way toward standardising the way that the PHP references get written.

Of course this isn't the first change toward standardising the PHP access as one particular short code had already been cross implemented into the way that is now almost the only way to switch to PHP.

Let's start by looking at the ways that PHP could be accessed in earlier versions of PHP. There were four. Two of these would always work while the other two could be turned on in the settings. The two that always worked were:

<script language=php> </script>
<?php ?>

The two short versions that could be turned on in the settings were:

<% %>
<? ?>

This last variant also provided a shortcut for echoing out a field that removed the need to use the echo keyword.

<?= $field ?>
It is this shortcut way of echoing a field that has also been excluded from the need to enable shortcodes and which will also work to echo PHP variables even when shortcodes are turned off.

Many of the people reading this will probably be surprised that there were so many ways to define the PHP code in the page and will only have been aware of one or two of these ways before seeing them all listed above. The reason for this is that for a long time very few people have bothered to use two of these ways. I can't remember seeing anyone's code examples where a full script tag was used to identify the PHP code or where the shortcode most commonly associated with ASP was used to identify PHP. Presumably those working on the PHP 7 standard hadn't seen those being used anywhere for a long time either because they decided that commencing with PHP 7 those two ways to access PHP would no longer be supported.

The <? ?> shortcut is still supported if you turn it on in the settings but except for old code that is yet to be converted to use the slightly longer variant, there is no real reason for using it any more. The extra short variant of adding an = to echo output now works ecen with shortcodes off and so you can still use that for creating HTML templates where you want to substitute values from PHP while using <?php ?> for the rest of your PHP.

Given these moves to clean up the way to switch between HTML and PHP I would expect the shortcode variant to also be removed in the not too far distant future leaving just the one standard way of switching between the two (with the exception of that special case for echoing fields.

Of course constantly swapping between HTML and PHP is not considered to be the best way to write your PHP either and so apart from the special case of PHP variable substitution into HTML templates people are moving toward having just the one wrapper around all of the content of their PHP files and echoing the HTML when needed.

This brings us to consideration of any characters before the opening < or after the closing > of our PHP wrapper. Any characters there are considered to be HTML and so any whitespace there will be added around the HTML that we are sending to the browser. Whitespace before the < can break our PHP by preventing our ability to send headers. We can usually tell very easily that there is nothing before the < because it appears first in our editor (unless it is proceeded by a byte order mark generated by our editor - in which case we have the editor settings set incorrectly for editing PHP). Spaces after tne end of the HTML will not cause any problems but are much harder to spot in most editors since many don't show the space until you try to add something after them. Of course the page starts in HTML mode so we need the code to switch us to PHP in order to create the page but we don't really need to switch back to HTML at the end of the file. We can therefore omit the ?> completely and just leave the page in PHP mode where any trailing white space will simply be ignored.

 

This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow
Donate