3

Possible Duplicate:
Are PHP short tags acceptable to use?

I was looking at some sample code and I noticed that the code used

<?=

instead of

<?php

as opening tags, the closing tags were the same as usual, but I was just wondering if there is any different functionality between the two? The file extension of the sample code is also .phtml instead of .php

Community
  • 1
  • 1
Carwyn Stephen
  • 134
  • 4
  • 23

5 Answers5

11

<?= is not the same as <?php

<?= is the same as <?php echo

<? is the same as <?php

LeeR
  • 1,609
  • 11
  • 29
  • It's not the same, because it's not always available, and therefore not portable. – Oliver Charlesworth Jul 27 '11 at 21:35
  • 1
    @Oli... It **IS** the same. He was asking for the difference or similarities. So to answer his question, they are the same, whether they are always available or not. – LeeR Jul 27 '11 at 21:38
  • It is indeed the same. I agree though that improving the answer by saying that the short tags are not always enabled could be great. Except that, short echo tag rocks, and your answer too. – Matthieu Napoli Jul 27 '11 at 21:41
6

<?= is shorthand for <?php echo ...

It's used like so:

<?=$var1?>

instead of:

<?php echo $var1; ?>
gen_Eric
  • 223,194
  • 41
  • 299
  • 337
2

From http://www.php.net/manual/en/language.basic-syntax.phpmode.php:

There are four different pairs of opening and closing tags which can be used in PHP. Two of those, <?php ?> and <script language="php"> </script>, are always available. The other two are short tags and ASP style tags, and can be turned on and off from the php.ini configuration file. As such, while some people find short tags and ASP style tags convenient, they are less portable, and generally not recommended.

...

Also note that if you are embedding PHP within XML or XHTML you will need to use the <?php ?> tags to remain compliant with standards.

So in summary, use <?php ?>.

Oliver Charlesworth
  • 267,707
  • 33
  • 569
  • 680
0

<?= prints the result of the PHP. It's roughly equivalent to <?php echo ...

<?php of course just starts the code block.

Cfreak
  • 19,191
  • 6
  • 49
  • 60
0

<?= is shorthand for <? echo

so you could write

Hello, <?= $name ?>.

instead of

Hello, <? echo $name ?>.
Austin
  • 711
  • 6
  • 3