241
<?php

$a=1;

?>
<?=$a;?>

What does <?= mean exactly?

SilentGhost
  • 307,395
  • 66
  • 306
  • 293
user198729
  • 61,774
  • 108
  • 250
  • 348
  • Side note: This is used extensively in ASP.NET MVC views. – Omar Jan 07 '10 at 14:26
  • 2
    http://stackoverflow.com/questions/1963901/what-does-this-symbol-mean-in-php/1963912 – Mike B Jan 07 '10 at 17:10
  • 19
    Note that the `;` is redundant; as the answers suggest this short-tag expands to an `echo` with a semicolon added to the end, as per the [php documents](http://php.net/manual/en/language.basic-syntax.phpmode.php). – not-just-yeti Oct 05 '15 at 01:25

8 Answers8

329

It's a shorthand for <?php echo $a; ?>.

It's enabled by default since 5.4.0 regardless of php.ini settings.

Gleb Kemarsky
  • 10,160
  • 7
  • 43
  • 68
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
62

It's a shorthand for this:

<?php echo $a; ?>

They're called short tags; see example #1 in the documentation.

Gleb Kemarsky
  • 10,160
  • 7
  • 43
  • 68
Will Vousden
  • 32,488
  • 9
  • 84
  • 95
25

Since it wouldn't add any value to repeat that it means echo, I thought you'd like to see what means in PHP exactly:

Array
(
    [0] => Array
        (
            [0] => 368 // T_OPEN_TAG_WITH_ECHO
            [1] => <?=
            [2] => 1
        )
    [1] => Array
        (
            [0] => 309 // T_VARIABLE
            [1] => $a
            [2] => 1
        )
    [2] => ; // UNKNOWN (because it is optional (ignored))
    [3] => Array
        (
            [0] => 369 // T_CLOSE_TAG
            [1] => ?>
            [2] => 1
        )
)

You can use this code to test it yourself:

$tokens = token_get_all('<?=$a;?>');
print_r($tokens);
foreach($tokens as $token){
    echo token_name((int) $token[0]), PHP_EOL;
}

From the List of Parser Tokens, here is what T_OPEN_TAG_WITH_ECHO links to.

Gordon
  • 312,688
  • 75
  • 539
  • 559
14

<?= $a ?> is the same as <? echo $a; ?>, just shorthand for convenience.

Jeffrey Aylesworth
  • 8,242
  • 9
  • 40
  • 57
8
<?=$a; ?>

is a shortcut for:

<?php echo $a; ?>
Inspire
  • 2,052
  • 15
  • 14
8

As of PHP 5.4.0, <?= ?> are always available even without the short_open_tag set in php.ini.

Furthermore, as of PHP 7.0, The ASP tags: <%, %> and the script tag <script language="php"> are removed from PHP.

Gaius Gracchus
  • 111
  • 1
  • 3
5

It's a shortcut for <?php echo $a; ?> if short_open_tags are enabled. Ref: http://php.net/manual/en/ini.core.php

Matteo Riva
  • 24,728
  • 12
  • 72
  • 104
4

I hope it doesn't get deprecated. While writing <? blah code ?> is fairly unnecessary and confusable with XHTML, <?= isn't, for obvious reasons. Unfortunately I don't use it, because short_open_tag seems to be disabled more and more.

Update: I do use <?= again now, because it is enabled by default with PHP 5.4.0. See http://php.net/manual/en/language.basic-syntax.phptags.php

aefxx
  • 24,835
  • 6
  • 45
  • 55
antihero
  • 423
  • 5
  • 10