Bigballs
Bigballs

Reputation: 3809

<?php instead of <?

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

Upvotes: 12

Views: 1739

Answers (10)

Darcy Casselman
Darcy Casselman

Reputation: 2644

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

This might be a religious question.

Upvotes: 0

sprugman
sprugman

Reputation: 19831

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.

Upvotes: 1

Michael Burr
Michael Burr

Reputation: 340228

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.

Upvotes: 2

spoulson
spoulson

Reputation: 21591

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.

Upvotes: 0

Peter Bailey
Peter Bailey

Reputation: 105918

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

Upvotes: 30

Mir Nazim
Mir Nazim

Reputation: 626

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

Upvotes: 3

stesch
stesch

Reputation: 7215

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.

Upvotes: 5

&#211;lafur Waage
&#211;lafur Waage

Reputation: 70001

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.

Upvotes: 7

Jeff Winkworth
Jeff Winkworth

Reputation: 4996

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

Upvotes: 1

Ray Booysen
Ray Booysen

Reputation: 30031

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

Upvotes: 3

Related Questions