1

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 place 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.

Ricalsin
  • 920
  • 9
  • 27

8 Answers8

2

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.

icktoofay
  • 117,602
  • 18
  • 233
  • 223
1

<?= 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?

Community
  • 1
  • 1
bcoughlan
  • 23,518
  • 17
  • 81
  • 132
  • Thanks for the link. In it they discuss the = as supported by Zend. It had a lot of links and thoughts on the subject, along with the php.net manual links posted on here. Thank you to all. – Ricalsin Sep 11 '11 at 04:53
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; ?>
Josh
  • 7,714
  • 5
  • 40
  • 40
1

"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"?>'; ?>
Joe
  • 15,062
  • 4
  • 38
  • 77
0

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

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

Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989
0

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.

0

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

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

0

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.

Brent Friar
  • 10,565
  • 2
  • 17
  • 30