[wp-meta] [Making WordPress.org] #4159: Short echo tag ("<?=") tag must be allowed for use in templates
Making WordPress.org
noreply at wordpress.org
Thu Feb 7 14:03:02 UTC 2019
#4159: Short echo tag ("<?=") tag must be allowed for use in templates
------------------------+-------------------------
Reporter: KestutisIT | Owner: (none)
Type: defect | Status: new
Priority: normal | Milestone: Q1
Component: Handbooks | Keywords: needs-codex
------------------------+-------------------------
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
<?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
1. **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.
2. That is why since PHP 5.4.0 the `<?=` tag is always available.
3. 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.
4. 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" ?>`.
5. Although **`<?`** causes conflicts with xml, **`<?=`** does not.
6. Prior to PHP 5.4 - the php.ini options to toggle it on and off were
tied to
[**`short_open_tag`**](http://www.php.net/manual/en/ini.core.php#ini
.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.
7. Starting from PHP 5.4 - 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.
8. 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](https://make.wordpress.org/core/2018/12/08/updating-the-minimum-
php-version/)".
9. 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 - `<?php` and 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 missing `esc_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
--
Ticket URL: <https://meta.trac.wordpress.org/ticket/4159>
Making WordPress.org <https://meta.trac.wordpress.org/>
Making WordPress.org
More information about the wp-meta
mailing list