[Coding] PHP Short Tags
Posted by Khatharsis on January 14, 2013
I’ve been studying PHP technical interview questions at a slow pace. But it’s better than nothing. Last week I dedicated time to reading through a compilation of questions and answers just to get an overview and feel for what I should know. I realized this is probably a good way to get a thorough beginner’s understanding of a particular programming language. PHP is sort of my pet language as it was one of the first “programming languages” I tinkered with (although at that time, I only used includes() – I was only in high school and more interested in design than development back then). Studying the technical interview questions have made me realize how much I don’t know about the language and how much it is fascinating me. For example, reading more thoroughly the concept of short tags explains why "<? /* code */ ?>" did not always work when I was working on various campus websites as an undergrad as the websites were hosted on a variety of servers with different php.ini files and configurations. So, I just wanted to share the concept of short tags and what I have gleaned from Q&A sites like StackExchange and stackoverflow.
The interview question that sparked this scavenger hunt was, “What do the special set of tags <?= and ?> do?”
The simplest answer is that the output is directly sent to the browser. For example, suppose I have declared earlier in the page:
$a = 'Hello, world';
The following code:
<?= $a ?>
Will print ‘Hello, world’ to the browser. It’s a shorthand form of echo.
Pretty neat, huh? Unfortunately, it does carry with it a set of problems. First, the general shorthand of <? and ?> (to replace the slightly lengthier <?php and ?>) causes conflicts with XML tags. The workaround is to specifically echo the XML tags from PHP. The second problem is related in that the general shorthand and the shorthand of echo were coupled together up to PHP version 5.4. In other words, if you are using 5.3 and disable shorthand, the shorthand of echo is also disabled. PHP 5.4 and above have decoupled the two shorthand forms and according to one post I have read, the shorthand of echo is actually preferred. (There are also rumors that it will be removed in PHP 6, but I haven’t delved very far into it.)
I won’t go into style or opinions, but I found this to be interesting enough to warrant a post. The nice thing about choosing to disable or enable shorthand forms is it’s all server/back-end. It becomes more of a software engineering question, whether it is better to enable or disable short_open_tag in php.ini and thus enable shorthand forms, rather than a design question in which multiple workarounds have to be coded to support various users. Basically, a back-end vs. front-end question. What is the likelihood of needing to transfer the PHP code to another server? What version of PHP will be you be using and supporting? If a future version of PHP changes the shorthand form (e.g., removal or recoupling), how much editing will need to be done? And so on.
More reading (not endorsing the individual pages, but more for personal exploration if you so desire):
–PHP Manual – php.ini with short_open_tag entry
–stackoverflow – one of many Q&A posts available, bit opinionated in answers but valid points/ideas are raised
–StackExchange – another Q&A post, but has a flowchart in determining when to use short tags