Ck Yong
Ck Yong

Reputation: 51

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

What is the difference between

<?php echo '$test'; ?>

and

<?=$test?>

?

Upvotes: 4

Views: 1627

Answers (4)

Yuriy
Yuriy

Reputation: 1984

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

Upvotes: 3

Jim
Jim

Reputation: 18853

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.

Upvotes: 1

Jordan S. Jones
Jordan S. Jones

Reputation: 13883

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.

Upvotes: 10

mpen
mpen

Reputation: 282885

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

Upvotes: 8

Related Questions