tags:

views:

293

answers:

8
+10  A: 

The first is called short-open tags and second one is safe open and close tags. You could enable/disable short open tags in php.ini using short_open_tag setting.

The short tags should be avoided, have a look at:

PHP Short Open Tag: Convenient Shortcut or Short Changing Security?

Sarfraz
"All of my PHP code was silently output directly to the browser, completely ignored by PHP! No runs, no hits, no errors." Of course not. All the code inside those short tags is ignored by PHP.
NullUserException
@NullUserException: Because you have short tags disabled from php.ini :)
Sarfraz
Using short tags is quite fine as long as you've got control over the environment they're used in. Short tags are only discouraged if you need to distribute the software, because then you no longer possess that control.
Daniel Egeberg
@Daniel Egeberg: Yes pretty obvious :)
Sarfraz
@Daniel Egeberg: I've often worked at the project level as part of larger organization, and I've seen a number of times when central IT consolidates (e.g. shuts down my local server and requires me to use a central server) in the name of "security" or "cost savings". Thus, my recommendation is to always take the path likely to result in the least re-work later, which would be `<?php` over `<?`.
GreenMatt
+6  A: 

Servers must be configured to also use <?, so it is considered best practice to use <?php for portability reasons.

From the manual ( 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, and , 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.

Scott Saunders
A: 

There is no difference language-wise, but many shop prefer the use of <?php because the simple <? opening tag can be found in XML files, which can lead to confusion for the interpreter.

Edit: I thought this was still an issue: http://terrychay.com/article/short_open_tag.shtml

Gabe
That is totally false.
NullUserException
Is this no longer the case (as described here: http://terrychay.com/article/short_open_tag.shtml)
Gabe
From the manual: "Note: 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."
Scott Saunders
Oh come on vote down for something that was true? laaame
Gabe
+1  A: 

I have never personally run into this issue, but support for <? ?> is spotty when moving to different servers. I prefer to just stick to <?php ?> for clarity and consistency.

krs1
+3  A: 

<?php can always be used. <? can only be used if the short_open_tag directive is turned on.

short_open_tag tells PHP whether the short form (<? ?>) of PHP's open tag should be allowed. If you want to use PHP in combination with XML, you can disable this option in order to use <?xml ?> inline. Otherwise, you can print it with PHP, for example: <?php echo '<?xml version="1.0"?>'; ?>. Also, if disabled, you must use the long form of the PHP open tag (<?php ?>).

Note: This directive also affects the shorthand <?=, which is identical to <? echo. Use of this shortcut requires short_open_tag to be on.

-- Description of core php.ini directives

As others have mentioned, this directive is often turned off so for portability reasons I prefer using <?php ?>. If this is not an issue, there shouldn't be much difference other than that if the directive is turned on you can also use the <?= shorthand thingy.

Svish
+1  A: 

Using short tags <? ?> should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server. For portable, redistributable code, be sure not to use short tags.

And 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.

Bakhtiyor
"... <?php ?> tags to remain compliant with standards."That is not exactly true. XHTML standards compliance of generated markup is the same, regardless of php tags used.
NullUserException
+1  A: 

Always use <?php ?> because <? ?>:

  • will not work in coming PHP versions
  • could be mixed with XML definitions. (XML always starts with <?xml ...)
  • is not enabled on many shared hosting sites.
Hippo
RE: "will not work in coming PHP versions" - Do you have documentation of this?
Scott Saunders
A: 

short tags <? ?> , only work in older php versions.

Dexah
Not true at all. See comments above.
John Conde