简体   繁体   中英

<?php instead of <?

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

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

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.

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

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

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.

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

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.

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

This might be a religious question.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM