Reputation: 3809
Why should I use <?php
instead of <?
in my php script?
Upvotes: 12
Views: 1739
Reputation: 2644
Wordpress Coding Standards says only use <?php
. They don't explain why.
This might be a religious question.
Upvotes: 0
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
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
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
Reputation: 105918
The two main reasons to avoid the short opening tag are
<?php
will work on every PHP enabled server, but <?
can be disabledUpvotes: 30
Reputation: 626
I personally prefer <?
to be switched off for two reasons
<?xml version="1.0" encoding="UTF-8" ?>
)<?php
is more readable and prominently visible than <?
.Upvotes: 3
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
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
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
Reputation: 30031
Both are semantically the same. <?
is simply a shortcut for the full syntax.
Upvotes: 3