开发者

difference <?php echo '$test'; ?> and <?=$test?>

What is the difference between

<?php echo '$test'; ?>
开发者_如何学Go

and

<?=$test?>

?


Assuming you really meant <?php echo $test; ?>, the two are effectively the same thing. The question is, how portable do you want to be. <?php ?> is supported just about anywhere that PHP is supported, however lots of admins disable <?= ?> syntax.


The former outputs the literal string $test and the latter outputs the value $test.


<?= $test ?> is identical to <?php echo $test; ?>

Since PHP 5.4.0 this <?= ... ?> tag is always available regardless of php.ini settings on short tags, and short_open_tag directive only controls <? ... ?> tag.

Also, relevant answer.

It is generally advised not to use short tags, but it is handy to use them only for simple outputs in templates with longer version only used for more complicated logic. That makes PHP code which tends to be messy more readable.


I tend to use the <?= ?>, which is called short_open_tags, for templates or the "view" portion of my scripts. But this does have to explicitly be enabled in the php.ini so if you are working on a script for distribution it is best to avoid using it, unless you do not care if the buyers / users of the script may not have short_open_tags turned on.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜