PHP Echo VS PHP Short Echo Tags

PHP echo vs PHP short echo tags

First of all, <?= is not a short open tag, but a shorthand echo, which is the same as <?php echo. And it cannot be disabled. So, it's safe to use in the meaning it is always enabled.

Speaking of safety in terms of security, the output must be always encoded according the the output medium rules.

For example, when echoing data inside HTML, it must be html-encoded:

 <?= htmlspecialchars($function_here, ENT_QUOTES) ?>

Or, when echoing data inside JavasScript, it must be javascript encoded:

 <script>var=<?= json_encode($function_here) ?>

Or, when it's going to be both HTML and JS, then both encodings must be used:

<?php foreach($links as $label => $url): ?>
<br>
<form method="post">
<button class="my" onclick="<?=htmlspecialchars("window.open(".json_encode($url).")", ENT_QUOTES) ?>">
<?=htmlspecialchars($label, ENT_QUOTES) ?>
</button>
</form>
<?php endforeach ?>

Speaking of short open tags, there is only one, <?, and it's not always enabled (see the short_open_tag directive).

Actually, in the php.ini-production file provided with PHP 5.3.0, they are disabled by default:

$ grep 'short_open' php.ini-production
; short_open_tag
short_open_tag = Off

So, using them in an application you want to distribute might not be a good idea: your application will not work if they are not enabled.

<?php, on the other side, cannot be disabled -- so, it's safest to use this one, even if it is longer to write.

What exactly is the short echo tag <?= ?>? How does it work? What is its advantage over <?php ?> tag?

If it's always available then why I got this error?

Because you put an echo statement after the <?=. You should just put the expression you want to echo.

The error said "unexpected echo", it isn't directly related to the short tags.

What exactly is the short echo tag <?= ?> ?

A tag to echo content.

How does it work?

It echos whatever expression you put inside it

What is its advantage over <?php ?> tag?

It is shorter

What is the necessary of it as there is already <?php ?> tag present?

It isn't necessary. It is convenient. Programming languages are there to make life easier for programmers. If they included only things that were necessary then we'd all be programming using assembly language.

is Short tag good practice in php?

Short tags <? doSomething(); ?> are considered to be a bad practice because they are not XML compliant... whether you care about that or not is another issue.

Short echos <?= $myString ?> are not a bad practice, it's just not the best. PHP is a templating engine, however much better engines are available (Twig, Mustache, Smarty, etc). Most frameworks include their own templating engine so short tags don't need to be used.

Up to and including PHP 5.3, these types of tags were considered to be the same thing. Since PHP 5.4 however they've been separated out and short echo is allowed without enable-short-tags being turned on. Since PHP 5.3 is no longer supported, the only concern is if you're being forced to use an unsupported version, which obviously has it's own implications. :)

Should I use . or , to separate variables in PHP short tag echo?

The operation you're performing is concatenation. Technically you can also use a comma as well, but for clarity I would use the concatenation operator, which is a period (or dot).

Using a comma seems to be slightly faster, but this kind of speed differences are negligible. Code should always be optimized for reading (it's hard to read by definition already), only when serious performance issues are encountered you can start optimization. And even then, replacing concatenation with passing multiple arguments is not going to improve much.

Also, this works only for the echo() function. Consistency is usually good thing.

P.S. Using a space after a comma or around operators is also often recommended for readability:

<?=$var1 . $var2?>
<?=$var1, $var2?>

Are the short echo tags intended to use only when PHP code is embedded into HTML code?

Yes you can use Short tag in a file which contains pure PHP code only. Here you are using php opening short tag inside php tags, that's why its throwing Parse Error.

if yo want to use, then you should try like below .

<?php

echo 'if you want to serve PHP code in XHTML or XML documents,
use these tags';
?>
<?= 'While this is going to be parsed.'; ?>

Why are echo short tags permanently enabled as of PHP 5.4?

Short open tags are not always enabled since PHP 5.4. The documentation talks about the short echo tags. Which is a different thing. (short open tags are <? style tags, short echo tags are <?= style tags, for echo-ing).

Then why are they enabled by default now? Well, there are a lot of scripts out there, where it benefits to use <?= $somevar ?> instead of <?php echo $somevar ?>. And because the short echo tags aren't as bad as the short open tags, they chose to always enable the short echo tags. Because now developers (of frameworks and CMS-es) can count on them (or rather, when PHP 5.4 becomes mainstream).

However, the short open tags are still influenced by the short_open_tag setting in your php.ini.

<?= and <? echo in PHP

There is no right or wrong for this, it's an opinion based matter. I personally do like to use shorttags when I can because I find it easier to read. Others might find the

 <?php

easier to read and will prefer this. It's a fully opinion based matter and I wouldnt worry about it. Use which you prefer.

Is it now safe to use PHP echo short tags?

I wouldn't use the words "totally" and "completely", but with PHP5.4 the "short-open-and-echo"-syntax is part of the core and thus always available. Remind, that I only talk about <?= ?> and not the "regular" short-open-tags <? ?>.



Related Topics



Leave a reply



Submit