History
Before the misinformation train goes too far out of the station, there are a bunch of things you need to understand about PHP short tags.
The primary issue with PHP's short tags is that XML managed to choose a tag (<?
) that was already used by PHP.
With the option enabled, you weren't able to raw output the xml declaration without getting syntax errors:
<?xml version="1.0" encoding="UTF-8" ?>
This is a big issue when you consider how common XML parsing and management is.
What about <?=
?
Although <?
causes conflicts with xml, <?=
does not. Unfortunately, the options to toggle it on and off were tied to short_open_tag
, which meant that to get the benefit of the short echo tag (<?=
), you had to deal with the issues of the short open tag (<?
). The issues associated with the short open tag were much greater than the benefits from the short echo tag, so you'll find a million and a half recommendations to turn short_open_tag
off, which you should.
With PHP 5.4, however the short echo tag has been re-enabled separate from the short_open_tag
option. I see this as a direct endorsement of the convenience of <?=
, as there's nothing fundamentally wrong with it in and of itself.
The problem is that you can't guarantee that you'll have <?=
if you're trying to write code that could work in a wider range of PHP versions.
ok, so now that that's all out of the way
Should you use <?=
?
echo
leads very easily to XSS, and you should better rely on context-dedicated echoing method (ie: having afunction html($x) { echo htmlentities($x,...); }
and unhtml($someVar);
instead ofecho $someVar
or usingecho json_encode($x);
for JS context). This then makes<?=
tags a bad practice because it means you have HTML-escaped the variable content in another place, and so that other place must magically know this variable has to be HTML escaped because it's echoed in HTML context.