tags:

views:

680

answers:

12
+10  Q: 

<?php instead of <?

Why should I use <?php instead of <? in my php script?

A: 

This is essentially a dupe of http://stackoverflow.com/questions/436688/difference-between-php-echo-sessionid-and-sessionid

gms8994
not really; that question is about shorttags (a quick way of echoing out variable contents), wheareas this is about the different tag conventions.
Jeff Winkworth
But the answers cause this to be a dupe.
gms8994
+3  A: 

Both are semantically the same. <? is simply a shortcut for the full syntax.

Ray Booysen
works exactly the same in JSP
Vinze
Hmm, it appears you edited your answer, so my comment is no longer true.
nickohrn
Nick. that code I showed was for ASP.NET MVC and was simply an analogy
Ray Booysen
But it was an incorrect analogy. PHP does provide a utility tag like ASP.Net does, but it is <?= as stated in my previous comment. You corrected your answer so I +1ed you.
nickohrn
:) Sorry was trying an analogy that obviously failed. :) Probably shouldn't have compared ASP.NET MVC which has the <%= for something different to PHP's <?=
Ray Booysen
+1  A: 

It should be noted that some server configurations of PHP do not support one or the other out of the box. For instance, my web server does not support <? but does support <?php

Jeff Winkworth
This is technically incorrect - ALL php installations support the normal (long) opening tag, whereas the short opening tag can be disabled. To say "some do not support one or the other" is factually incorrect.
Peter Bailey
+6  A: 

There are two methods to start PHP

<?php

and

<?

PHP also includes

<?=

But that will print out the variable behind it, since its a shorthand for echo.

You should use < ?php since its compatible with all server types. You can move your code to a new hosting solution or setup a new server default and the shorthands could be disabled.

Ólafur Waage
+1  A: 
There's a button at the top of the answer editor (looks like 1s and 0s) for code. Try that!
notruthless
I REALLY hope this doesn't happen. PHP sucks enough already without adding needless bloat by enforcing <?php everywhere
Orion Edwards
+4  A: 

The short tags <? (and <?=) could be disabled. The long version <?php (and <?= = <?php echo) work everywhere.

We could discuss the sense of this decision of the PHP team to make such a thing configurable, but the essence is, that you can't use the short tags in portable code.

stesch
why on earth would anyone disable the short tags?
Orion Edwards
@Orion Edwards: It's a config option. php.ini makes it really difficult to write portable code. More difficult than necessary. If it's an option, then it's taboo for portable code. :-(
stesch
A: 

Wordpress Coding Standards says only use <?php. They don't explain why.

This might be a religious question.

Darcy Casselman
<?php makes code more portable, that's why WordPress Coding Standards recommend it.
nickohrn
+3  A: 

I personally prefer <? to be switched off for two reasons

  1. PHP Coding Standards across many big PHP project recommend it to be switched off
  2. Creates problems if you are generating XML docs(xml docs begin with <?xml version="1.0" encoding="UTF-8" ?>)
  3. (bonus) <?php is more readable and prominently visible than <?.
mirnazim
#3 is a MISfeature. I want PHP to get out of my damn way, not make itself prominently visible
Orion Edwards
Your point 2 makes you a bozo: http://hsivonen.iki.fi/producing-xml/
stesch
+25  A: 

The two main reasons to avoid the short opening tag are

  1. Maximum portability - <?php will work on every PHP enabled server, but <? can be disabled
  2. It's identical to the opening of the XML preamble - could cause parsing or execution headaches
Peter Bailey
The XML argument doesn't count. (http://hsivonen.iki.fi/producing-xml/). The XML argument is in the same category as deciding on a non-standard way to escape SQL strings. The PHP team made a big mistake and accidently killed this short tag feature.
stesch
Any server admin that disables <? should be shot.
Jasper Bekkers
Jasper, please don't advocate shooting people unless you're willing to look the target in the eye and pull the trigger yourself.
Rob Kennedy
If by "shot" you mean "given a shot of his favorite liquor" then I agree.
Peter Bailey
OK. Perhaps shot is too strong a word, but punched, definitely
Orion Edwards
A: 

The support of the short form <?/<?= syntax is based on the short_open_tag parameter in php.ini. If you host your own codebase, this is trivial to enable. However, on a shared/managed web host, you may not have control over this setting.

Thus, for maximum portability you should use the long form syntax.

spoulson
+1  A: 

To add to everyone's explanation that short tags should not be used because they might be disabled (or maybe even removed from future PHP versions), the main rationale for short tags to be deprecated is that they are not valid XML "Processing instructions" while the <?php tags are. So PHP templates with <?php ... ?> tags can be valid XML documents while those using the short tags are not.

Michael Burr
+1  A: 

The portability arguments are all well and good, but I'm going to be sacrilegious and say that you should go ahead and use short tags if you want to, unless you're doing something that is for really wide distribution (of the source), or are making something very xml focused.

a) I've never been in a situation where I couldn't enable short tags. Usually they're on by default.

b) In most situations, the <? is not going to get confused with <?xml because they're being parsed at different levels of the stack.

c) the short tag is less obtrusive and therefore clutters your code less. I find this especially true with <?php echo vs. <?=. (You could argue that you should generally avoid using either one of those, because it probably means you're writing spaghetti code, but come on -- this is php!)

Of course, it all depends on what kind of project you're working on, but for a basic web site, I don't think the short tag is at all likely to hurt you.

sprugman
Yet in the future PHP short tags will not be enabled and all of your code will break with the next major release.
X-Istence
Is that true? Is there an official source for that?
sprugman
On php.net, I see a recommendation to use long tags, which is fine, but I don't see anything official about short tags going away.
sprugman