2

Possible Duplicates:
What is the difference between the PHP open tags “<?=” and “<?php”/“<?”?
Reference - What does this symbol mean in PHP?

Is there really any difference in writing the syntax like <?php ?> or as <? ?>.

I was under the impression that it really didn't matter as long as the file had an .php extension. But sometimes I seem to run into problems with the syntax.

so whats the lowdown?

Community
  • 1
  • 1
digster
  • 392
  • 5
  • 20

3 Answers3

2

The latter is called shorttags.

You can disable those tags in php's configuration, apart from that there is not really a difference.

Because you can disable them, you should not use them, as they can cause lots of unexpected problems when you happen to run your scripts on servers where you can't enable them. Another reason, as noted by Sjoerd in the comment is, that they can cause conflicts when you have an XML header for instance.

If you enable short tags though, you get a shorthand syntax for the echo function: <?= $string ?>

It's enabled using the short_open_tag setting in php.ini. You can read about it here.

Update As of PHP 5.4.0 alpha1:

<?= is now always available regardless of the short_tags setting (Rasmus)

phant0m
  • 16,595
  • 5
  • 50
  • 82
  • Indeed. You should always use the long version, in case shorttags is disabled on a server you want to deploy on. Also, take this into account when writing an `` header. – Sjoerd Jun 22 '11 at 19:54
2

The below will work on all servers.

<?php // some logic in here ?> 
<?php echo "cat"; ?>

The below will work on servers that have their php.ini properly configured for short tags.

<? // some logic in here ?>
<?= "cat" ?>
Steve Nguyen
  • 5,854
  • 5
  • 21
  • 39
  • so i guess thats why sometimes the aforementioned problems arose because of the latter syntax.... – digster Jun 22 '11 at 19:55
  • @user597272 correct, please vote my answer correct and up vote me if this is good. I survive off of points on here. – Steve Nguyen Jun 22 '11 at 19:56
0

one little dangerous thing about shorttags is that if the php.ini is set incorrectly your sourcecode is available on the internet. so

Gidon Wise
  • 1,896
  • 1
  • 11
  • 11