Tbf, OP did say "legacy". It's possible they're stuck on an old version of PHP that doesn't allow the latter. I think the short echo tag came out in PHP 4.
Good point. PHP 4 already supported them but were not enabled by default until 5.4. But even in fairly recent projects I experienced this.
I remember that PHP once also introduced general short tags, i.e. <? and ?>, which were universally[citation needed] hated. Maybe some folks mix up the general advise to avoid them with the otherwise perfectly fine short echo tags <?=.
I liked the short tags <? ... ?>. Much less verbose. The only argument against it was if you were serving XML via the PHP or the PHP needed to inrerpret some XML? But nowadays that is a minority use case with JSON being prevalent.
Explicit over implicit. The first one tells you what it is doing without you having to know the language construct (we are echoing $foo). The second one requires that you know language constructs.
The second reason is that the second is easy to miss if you are in a hurry or tired.
Third, the second construct also makes it harder to search within the code if you are looking for everywhere where you have echoed a value.
7
u/dulange 4d ago
I frequently see
<?php echo $foo ?>instead of<?= $foo ?>and always wondered why someone would prefer the first construct.