3

Possible Duplicate:
Are PHP short tags acceptable to use?

We are currently using a mix of both styles for writing php code inside our phtml files. Nothing seems to be breaking but I'm just curious about using one over another.

(looking for something more then.. 'its best practice' answer)

Community
  • 1
  • 1
Adi
  • 187
  • 1
  • 14

7 Answers7

8

<? ?> are called short tags, they need to be enabled on the server in the php ini file. The only reason why you should not use them is because of compatibility, which in my opinion is not that big a deal, but if you are writing a CMS that is supposed to be used by other people on other servers you should use normal tags <?php ?>

Felix Kling
  • 795,719
  • 175
  • 1,089
  • 1,143
Antwan van Houdt
  • 6,989
  • 1
  • 29
  • 52
  • 2
    I'm curious as to why you think that compatibility is not a "big deal"? Indeed, with a system such as the World Wide Web which, by definition, is accessible by billions of people across the world through all manner of technologies, compatibility is a _huge_ deal. – Lightness Races in Orbit Apr 27 '11 at 22:10
  • 3
    If you have control over your own server, then why is being compatible with mythical other servers that don't run your code matter? – Matt Greer Apr 27 '11 at 22:24
  • @TomalakGeret'kal if the script only runs on your OWN server compatibility is not a big deal as YOU control the server @MattGreer it could be his scripts aren't only meant to run on his own server, like an opensource project or a system like swiftpanel – Antwan van Houdt Apr 27 '11 at 22:36
  • You didn't specify that criterion in your initial assertion that compatibility "is not that big a deal". :) – Lightness Races in Orbit May 07 '11 at 16:44
  • @MattGreer: To make life not ridiculously difficult on the day when you inevitably find yourself porting to a different server. Unless you can predict the future and know that you'll only _ever_ use this specific machine for this codebase. – Lightness Races in Orbit May 07 '11 at 16:44
5

The <? ?> syntax needs to be set to "on" in php.ini, so it may not work

<?php ?> always works.

user229044
  • 232,980
  • 40
  • 330
  • 338
Ale
  • 433
  • 3
  • 8
5

Always available:

  • <?php & ?> (Most Common)
  • <script language="php"> & </script> (Least Common)

Enabled by short_open_tags php.ini directive:

  • <? & ?> (Short Tags) allows for <?=$var;?> so you can echo.
  • <% & %> (ASP Style)

When I release code, I always use the <?php & ?> as it's supported across the whole ecosystem of PHP. If you use the short open tags and you distribute the code, then there is a chance that some clients will not be able to use it.

user229044
  • 232,980
  • 40
  • 330
  • 338
Mark Tomlin
  • 8,593
  • 11
  • 57
  • 72
  • 2
    There is a chance that some installations of PHP may not have GD, or MySQL or any number of optional PHP components. If you stick to only features "guaranteed" to be included, you're going to be writing pretty useless scripts. – user229044 Apr 28 '11 at 01:41
  • @meagar: Is is true that some things can be expected, but for something like this, something like that should not be taken into consideration. When I release code, I never have a problem with people not having GD, or MySQL, I do however have a problem with them not enabling the short tags. And basically, what is so wrong about building compatible scripts from the git go? – Mark Tomlin Apr 28 '11 at 15:40
  • Avoid a useful feature because somewhere somebody might have mis-configured their server to disable that feature is silly. I'm not advocating the use of short tags in redistributable libraries, where each file should contain exactly one opening `` statements is hideous and counter-productive. PHP is only useful as a templating language if you take short tags as a given. – user229044 Apr 28 '11 at 16:55
  • 1
    @meager: It may be _less convenient_ to write them out in full, but I can hardly see why it would be not "useful". – Lightness Races in Orbit Apr 29 '11 at 01:09
  • It should be noted that as of PHP 5.4 the short syntax for echoing `=$var;?>` is always available regardless of the setting within short_open_tags. – Mark Tomlin Jan 14 '13 at 14:39
4

<?=$var?> is the only sensible way to output a variable if you're using PHP templates. Writing each variable on the page as <?php echo $var ?> is a pain. So, if you're a masochist (or using different template), go for the long form, otherwise stick with short (at least in templates)

Your Common Sense
  • 156,878
  • 40
  • 214
  • 345
  • 3
    @Tomalak - **Be nice**. If you have a technical disagreement with an answer, discuss it in a _professional_ manner. I've edited your comment accordingly. – Tim Post Apr 27 '11 at 23:58
  • +1 For the comment about it being fine to mix PHP with HTML, especially on a view. I'm not convinced by the need to use short tags though. I always use `` for consistency and readability. – Dan Blows Apr 28 '11 at 00:21
  • 1
    @Tomalak You are absolutely incorrect. There is nothing wrong with writing your templates in PHP, that is what PHP is for. **PHP IS A TEMPLATE LANGAUGE**. Its express purpose is for generating HTML. It is a particularly awful template language without the use of short tags. Nobody wants to write `` every time they output a variable. – user229044 Apr 28 '11 at 01:35
  • @TimPost: Sorry if you feel that I didn't. I can't comment further on the issue since the comment seems to have vanished. I vaguely remember merely making a _strong_ point on the matter. – Lightness Races in Orbit Apr 29 '11 at 01:08
  • 1
    @meagar: And C++ is first and foremost a procedural language; that doesn't stop the use of objects from being highly recommended in order to promote modularity and re-usability. The same applies here. – Lightness Races in Orbit Apr 29 '11 at 01:08
  • 5
    @Tomalak No, C++ is first and foremost a multi-paradigm language with a focus on OOP, but that is completely irrelevant and your analogy is nonsense. Short tags have no C++ analog; there is no feature of C++ which is recommended against because somebody might turn that feature off, breaking your code. Rather, you use the tools you need to do your job, and tell people "*these* are the dependencies of my program." It's ludicrous to write software using only what you can guarantee every potential installation will support. – user229044 Apr 29 '11 at 03:17
  • @meager: In general and in principle I agree. I'm not suggesting that software should only use what one can guarantee will be universally available; of course all software has its stated dependencies and that's fine. But that fact is that `short_tags` are _disabled by default_, and there is a reason for that. – Lightness Races in Orbit May 07 '11 at 13:44
1
  • <? is also used for other script languages - if you have more than one installed on the server, it can lead to problems.
  • <? is disabled in some server configurations (short_open_tags in the php.ini), so if your script should run on other servers, use always <?php
Florian
  • 3,145
  • 1
  • 27
  • 38
  • what does it mean "if you have more than one installed on the server" and "it can lead to problems"? got an example? – Your Common Sense Apr 27 '11 at 22:16
  • 2
    What are you doing running your files through multiple interpreters? If that's actually happening on your server, you have more serious problems than short open tags – user229044 Apr 28 '11 at 01:38
1

<?php /* .. */ ?> is canonical.

<? /* .. */ ?> is "allowed" but certainly not recommended. This feature is enabled by setting short_open_tags to true in your php.ini, which hasn't been default for a while.

The rationale is out of scope of this question; there's plenty of documentation out there.

In particular, see this question.

Community
  • 1
  • 1
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
0

<? must no longer be used as explained here.

Community
  • 1
  • 1
Aif
  • 11,015
  • 1
  • 30
  • 44
  • 3
    You realize that the cited discussion is based on entirely wrong information? (PHP6 no longer removes short_open_tags. Well, if there ever is a PHP6.) – mario Apr 27 '11 at 22:12
  • Well, there are lots of elements in the answer of that other topic, this is why I linked it here, but yes, it's a shortcut I admit. – Aif Apr 27 '11 at 22:19
  • 1
    -1 for being absolutely wrong and disseminating completely incorrect information. Short tags were never on the chopping block; they're not going anywhere. – user229044 Apr 28 '11 at 01:39
  • such a bad answer, that the topic was closed giving my link as an alias. But I may have missed something. – Aif Apr 28 '11 at 07:55
  • Yes, you regurgitate the same incorrect information as some of the answers in the duplicate. That doesn't somehow justify giving an incorrect answer. – user229044 Apr 28 '11 at 16:57