Why should I use <?php
instead of <?
in my php script?
views:
680answers:
12This is essentially a dupe of http://stackoverflow.com/questions/436688/difference-between-php-echo-sessionid-and-sessionid
Both are semantically the same. <?
is simply a shortcut for the full syntax.
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
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.
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.
Wordpress Coding Standards says only use <?php. They don't explain why.
This might be a religious question.
I personally prefer <?
to be switched off for two reasons
- PHP Coding Standards across many big PHP project recommend it to be switched off
- Creates problems if you are generating XML docs(xml docs begin with
<?xml version="1.0" encoding="UTF-8" ?>
) - (bonus)
<?php
is more readable and prominently visible than<?
.
The two main reasons to avoid the short opening tag are
- Maximum portability -
<?php
will work on every PHP enabled server, but<?
can be disabled - It's identical to the opening of the XML preamble - could cause parsing or execution headaches
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.
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.
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.