266
<?php $a=1; ?> <?=$a;?> 

What does <?= mean exactly?

3
  • 1
    Side note: This is used extensively in ASP.NET MVC views. Commented Jan 7, 2010 at 14:26
  • 2
    stackoverflow.com/questions/1963901/… Commented Jan 7, 2010 at 17:10
  • 23
    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. Commented Oct 5, 2015 at 1:25

8 Answers 8

358

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

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

Sign up to request clarification or add additional context in comments.

4 Comments

Is it available in php 7?
Note that the final semi-colon is not required. But if, for whatever reason, you need to go back to classic tags, a simple project-wide search/replace <?= for <?php echo would'nt be enough. Anyway as of today I hope you guys doesn't have any production servers running PHP < 5.6 as this versions are not maintained anymore (PHP Supported Versions).
Important note: php echo shorthand (<?=) is not bound to short_open_tag directive! It can be used even if short_open_tag is disabled. you can't disable echo shorthand (<?=) at anyway.
@Fatih: Indeed. That was introduced in 5.4.0. See last paragraph of answer.
64

It's a shorthand for this:

<?php echo $a; ?> 

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

Comments

26

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.

1 Comment

The token failed to tell me more details.
14

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

Comments

8
<?=$a; ?> 

is a shortcut for:

<?php echo $a; ?> 

Comments

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.

Comments

5

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

Comments

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.