What does a special set of tags <?= and ?> do in PHP?

Dani AI

Generated

Short answer: <?= ... ?> is the short "echo" tag — a concise way to output an expression. Since PHP 5.4 the short-echo form is always recognized (it is no longer tied to the short_open_tag setting). The short_open_tag ini option only controls the plain short open <? form, not <?=. See the PHP manual for the language tags and the short_open_tag directive. (php.net)

Clarifications for this thread: was right that <?= is shorthand for echo, but not that it always required short open tags — that changed in PHP 5.4. s note about PHP 6 is also misleading: the PHP6 Unicode effort was abandoned and PHP6 was never released; many features were folded into later 5.x/7.x work. There were RFCs proposing to deprecate the <? short-open form, but those efforts did not remove the language feature outright. (php.net)

Practical advice: for maximum portability use full <?php for logic and feel free to use <?= in templates on modern PHP installs. Avoid the plain <? form in shared or XML-containing templates because it can be disabled or conflict with <?xml ... ?>. Also prefer omitting the PHP closing tag at the end of pure-PHP files to avoid accidental trailing-output. If supporting very old hosts, check or change short_open_tag in php.ini. (php.net)

References: PHP manual: PHP tags (tags), php.ini short_open_tag, History of PHP, RFC: deprecate short open tags (wiki).

Recommended Answers

All 5 Replies

I did a search and found an article that explains it (see link below).
<?= is a shortcut for echo. It only works if PHP's short open tags is turned on. This isn't much of a shortcut (especially since you also need the closing tag for a total of 5 characters to replace 5 if you count the closing semi-colon for the echo). It might be better termed as an alternative.

The ?> is the standard PHP closing tag. Normally, your code will be something like:

<?PHP
... a bunch of PHP code ...
?>

If you use the short open tags (not recommended) then this would be:
<?
... a bunch of PHP code ...
?>

You can also break in and out of PHP (by enclosing the PHP in open and closing tags wherever it appears) and include HTML / Javascript code in the same module.

http://perishablepress.com/press/2009/01/12/php-short-open-tag/

To elaborate on that Below are three examples of doing the same thing

<?="Hello World!"; ?>
<? echo "Hello World!"; ?>
<?php echo "Hello World!"; ?>

Now as you should know they all do the same thing providing php is configured correctly. However as of Php 5.3.0 in xampp the <? command is depreciated and instead you must use the <?php method. This means by default you would need to use the echo statement unless you go into the php.ini settings and enable short tags. But then as of Php 6.0 short tags are completely removed from php making it impossible to enable it again. So if you wish for your code to be compatible and php 6 compliant then the only option is to use the following method.

<?php echo "Hello World!"; ?>

Now a lot of people reading this might say wtf php 6 hasn't been released yet. Well php 6 has been documented at the php.net website and the expectations of php 6 have already been released but not the C/C++ code itself.

<?php echo "Hellow World"; ?>

is the same as:

<?= "Hello World" ?>

<?php echo "Hellow World"; ?>

is the same as:

<?= "Hello World" ?>

Except <?= "Hello World" ?> is depreciated.

Member Avatar for Member #120589

Deprecated tags. You gotta love 'em. I see your original post has attracted a number of neg reps. I assume members think that you should have used Google or looked up the php manual before posting.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.