Are there any disadvantages on using PHP Short Tags? I found that it makes my templates slightly more tidy by simplifying open tags and echos.
For example, anything wrong with this snippet?
<? if ( $page->hasImages() ) : ?>
<? foreach ( $images as $image ) : ?>
<img src="<?= $image->url(); ?>" alt="<?= $image->title()->or($page->title().' Project Image'); ?>" itemprop="image">
<? if ( ! $image->caption()->empty() ) : ?>
<?= $image->caption()->html(); ?>
<? endif; ?>
<? endforeach; ?>
</section> <!-- end section.images -->
<? endif; ?>
In PHP 5.4 the echo “short tag” (
<?=) is permanently enabled regardless of short_open_tag settings now. So it would be save to use
<?= since Kirby has dropped PHP 5.3 support in 2.2.
As regards the opening short tag
<?, I guess that is enabled in most hosting environments. For the widest compatibility of your code, avoiding the open short tag is still your best bet. There’s also supposed to be a possible conflict issue with XML.
Nice! I love my short echo tags
Since this is on a server with PHP 5.6 and won’t be open source, I’ll keep my short tags. But thanks for the information. If I ever make a plugin or a theme, I’ll know to use the traditional long tags.
Here’s a compact “flow chart” to make a decision. If you can answer any of the questions with “yes”, you shouldn’t use short tags (
- Are you creating a theme for other people to use?
- Are you building a site you don’t fully control the hosting of?
- Are you working in a team and don’t want to add another rule to your coding styleguide?
- Do you need to have XML in your templates?
I generally don’t recommend using short tags as the advantages don’t often outweigh the disadvantages. But I do recommend using the short echo tags (
<?=), they are very awesome and quite easy to understand.
I agree completely with @lukasbestle’s remark.
I certainly do use short echo tags
<?= ?> but never use short PHP tags
<? ?>. Even if I have 100% control over the server, I still want it to be more portable in the future, if we ever change hosting or if I no longer work with the site (by my client’s or my choice), and also for local development across machines or other dev’s local environments.
Best to keep it as portable as possible. One less thing to deal with when migrating the site.
Additionally, having the
<?php visible in the templates gives a good visual cue that you’re working with a control structure instead of an
echo. Just makes it that much easier and faster to visually scan the template file.