<?php
$a=1;
?>
<?=$a;?>
<?=
的确切含义是什么?
因为重复说明它的意思是echo
没有什么价值,我想你会喜欢看看在PHP中它准确的意思:
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
)
)
您可以使用以下代码进行测试:
$tokens = token_get_all('<?=$a;?>');
print_r($tokens);
foreach($tokens as $token){
echo token_name((int) $token[0]), PHP_EOL;
}
<?= $a ?>
和 <? echo $a; ?>
是等价的,只是前者是后者的简写形式,以方便使用。
<?=$a; ?>
是以下内容的缩写:
<?php echo $a; ?>
<?= ?>
始终可用。<%, %>
和脚本标签<script language="php">
已从 PHP 中删除。如果启用了short_open_tag
,则它是<?php echo $a; ?>
的快捷方式。 参考:http://php.net/manual/en/ini.core.php
<? blah code ?>
是相当不必要的,容易与XHTML混淆,但是显然 <?=
不会。不幸的是,我不使用它,因为 short_open_tag 看起来越来越被禁用了。<?=
了,因为它已经在 PHP 5.4.0 中默认启用。
详见http://php.net/manual/en/language.basic-syntax.phptags.php。
echo
语句,根据PHP文档。 - not-just-yeti