开发者

What is the PHP syntax "<?="

Reading some PHP code and I cannot understand the usage of:

<?=

I understand the php tag use of:

<?php and <?

The exact usage I am reading is:

<?=$form->hidden('mode',$mode); ?>

where $form is a new instantiated class of an object and "hidden" a class method. All of that is understood, but when I plac开发者_开发技巧e even a space between the ? and = of <?= I get an error of:

Parse error: syntax error, unexpected '=' in /home/...

I cannot find anything on google or php.net regarding the syntax.

Any help would be appreciated.


It's a shortcut for echo. Something like this (where expr is any expression):

<?= expr ?>

is functionally equivalent to this:

<?php echo expr; ?>

It's not recommended to use this syntax as it's not enabled on some servers. (and if someone is using shared hosting, they may not be able to re-enable it, either)

The PHP documentation on it is here.


<?= is shorthand for <?php echo. Its usage isn't recommended any more, apparently because of some ambiguity with the parser or incompatibility with some PHP versions, though I still see it in big projects like Drupal.

Edit: They're called "short tags" and a discussion on whether they should be used or not can be found here: Are PHP short tags acceptable to use?


"Short open tags" are hell, in short. If you have short_open_tags on in your php.ini / httpd.conf / .htaccess and you try to put an XML tag into your document, like so...

<?xml version="1.0" encoding="iso-8859-1"?>

PHP will think you tried to write some PHP, error, and die. You end up having to do

<?php echo '<?xml version="1.0" encoding="iso-8859-1"?>'; ?>


It is called the short open tag. If it is enabled on your server, then you may use it.

From PHP Manual:

This directive also affected the shorthand <?= before PHP 5.4.0, which is identical to <? echo. Use of this shortcut required short_open_tag to be on. Since PHP 5.4.0, <?= is always available.

The use of these type of tags allow you to receive the same result as using echo. The following examples are equivalent.

Example:

<?php echo $text; ?>
<?= $text; ?>


<?=, when short_tags is enabled, is a shorthand for <?php echo.

This is covered right at the start of the PHP manual.


Just a shorthand to quickly print content when, for example, you are in an HTML file. Its usage is not recommended since it is deprecated.


<?=stuff here;?> is a shorthand of <?php echo stuff here;?>

http://mattsblog.ca/2007/07/26/tip-of-the-day-php-shorthand/


That is a short tag and is not available for use on all servers.

<?= is the same as <?php echo

Putting a space in <?= invalidates the short tag which is why you get your error.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜