Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

PHP

Why is the use of short echo tags omitted from all PHP courses?

One thing I noticed in all PHP and PHP-related (ie Wordpress) lessons, the use of short echo tags are completely omitted, without even a mention.

In all examples, the use of the echo statement, in conjunction with the traditional php script tags are used, such as in:

<?php echo $var ?>

Using short echo tags, it is possible to rewrite the above as:

<?= $var ?>

Which can make the code a little shorter and neater.

Yes, previously you had to have short tags enabled in your server settings, but as long as you are running PHP 5.4 or newer, <?= ?> is enabled even if <? ?> is not.

1 Answer

Because not all web servers setup with PHP support the short tags. PHP 5.4 and greater does support short tags on default, but there are still many servers setup that may not support short tags. So in order to make sure your PHP code is portable, many examples use "echo" to ensure portability.

In fact, I,m running PHP 5.6 on my Debian computer and short tags are off by default.

According to the PHP documentation:

This directive also affected the shorthand <?= before PHP 5.4.0, which is identical to <? echo. Use of this shortcut required short_open_tag to be on. Since PHP 5.4.0, <?= is always available.

So perhaps while the feature is not explicitly set, the feature will work regardless on versions 5.4+.