-
-
Notifications
You must be signed in to change notification settings - Fork 522
Description
In the MVC+Templating world, all new code does have template separation from the code.
So in templates with template-like syntax, which uses "if(STATEMENT):", "endif;", "foreach(..):", endforeach;" - and that does make the code more compact and easier to read for web-designers, the short echo tag - "<?=" have to be allowed as well. It as endorsed by the Rasmus Lerdorf - the creator of the PHP language - that's why it is always available since PHP 5.4.0. And WordPress is officially announced that it is dropping the support of all older versions prior PHP 5.6.0 since this April.
So in a template file with template-like syntax, i.e.
/wp-content/plugins/<PLUGIN_NAME>/UI/Templates/Admin/Item/Shared/ItemsPartial.php
this should be allowed:
<?php foreach($items AS $item): ?>
<div class="item-title"><?=esc_html($item['item_title']);?></div>
<div class="item-description"><?=esc_br_html($item['item_description']);?></div>
<?endforeach; ?>And in this the Core Contributors handbook there has to be changes made to the Shorthand tags section: https://make.wordpress.org/core/handbook/best-practices/coding-standards/php/#no-shorthand-php-tags
Motivation
-
The creator of PHP language - Rasmus Lerdorf - endorses the shorthand
<?=tag. He claimed that in public in one of his talks on the (then soon to be released) PHP 5.4. If not the Rasmus, we would never have the PHP as most popular language in the world, and it became so popular just because of the Rasmus needs to get things start easy, easy to read, and feel happy - all the coding standards has to help us, not to make us hate them. -
That is why since PHP 5.4.0 the
<?=tag is always available. -
It was mostly done because by over-viewing many of projects with pre-PHP 5.4, the
<?=tag is used in the View of an MVC application but<?php ... ?>is used in the non-view files. -
The primary issue with tag (
<?) was because was used by another syntax, XML. With the option enabled, you weren't able to raw output the xml declaration without getting syntax errors for this code:<?xml version="1.0" encoding="UTF-8" ?>. -
Although
<?causes conflicts with xml,<?=does not. -
Prior to PHP 5.4 - the php.ini 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. -
Starting from PHP 5.4 - the
short echotag has been re-enabled separate from theshort_open_tagoption. I see this as a direct endorsement of the convenience of<?=, as there's nothing fundamentally wrong with it in and of itself. -
Last December WordPress core contributor officially announced that starting this April, WordPress is dropping the support for all older versions prior PHP 5.6.0. For more information please read the post at w.org named "Updating the Minimum PHP Version".
-
The oldest version of PHP that still gets security fixes is the PHP 5.6. In 2020 the majority of hosting providers and stacks like XAMPP will start offering only offer the PHP7. The PHP.net fully supports only PHP 7.2+ as of today.
Additional notes
-
The BAD is only the
<?tag. -
The GOOD are both -
<?phpand the<?=tags. Just first one is dedicated to use on code files, and the second one - in template files to output quickly single elements in a compact & easy-to-read, manner. -
If someone has a question regarding the
esc_br_html(...)- the ticket about the missingesc_br_html(...)function is here: https://core.trac.wordpress.org/ticket/46188 -
A good read about the short echo tags is this accepted answer at Stackoverflow with 186 up-votes: https://softwareengineering.stackexchange.com/a/151694/182409