<? or <?php --- is there any difference? [duplicate]
Possible Duplicates:
Are PHP short tags acceptable to use? What does “<?=” mean when seen in PHP
is there any difference in using <? ?>
to signify a php block, or using <?php ?>
?
if there is not, why would anyone use <?开发者_运维知识库php
?
figure the file extension of .php would give plenty of info about what type of code you are looking at.
The first is called short-open tags and second one is safe open and close tags.
You could enable/disable short open tags in php.ini using short_open_tag
setting.
The short tags should be avoided, have a look at:
PHP Short Open Tag: Convenient Shortcut or Short Changing Security?
Servers must be configured to also use <?
, so it is considered best practice to use <?php
for portability reasons.
From the manual ( http://www.php.net/manual/en/language.basic-syntax.phpmode.php ):
There are four different pairs of opening and closing tags which can be used in PHP. Two of those, and , are always available. The other two are short tags and ASP style tags, and can be turned on and off from the php.ini configuration file. As such, while some people find short tags and ASP style tags convenient, they are less portable, and generally not recommended.
<?php
can always be used. <?
can only be used if the short_open_tag directive is turned on.
short_open_tag tells PHP whether the short form (
<? ?>
) of PHP's open tag should be allowed. If you want to use PHP in combination with XML, you can disable this option in order to use<?xml ?>
inline. Otherwise, you can print it with PHP, for example:<?php echo '<?xml version="1.0"?>'; ?>
. Also, if disabled, you must use the long form of the PHP open tag (<?php ?>
).Note: This directive also affects the shorthand
<?=
, which is identical to<? echo
. Use of this shortcut requires short_open_tag to be on.-- Description of core php.ini directives
As others have mentioned, this directive is often turned off so for portability reasons I prefer using <?php ?>
. If this is not an issue, there shouldn't be much difference other than that if the directive is turned on you can also use the <?=
shorthand thingy.
I have never personally run into this issue, but support for <? ?>
is spotty when moving to different servers. I prefer to just stick to <?php ?>
for clarity and consistency.
Using short tags <? ?>
should be avoided when developing applications or libraries that are meant for redistribution, or deployment on PHP servers which are not under your control, because short tags may not be supported on the target server. For portable, redistributable code, be sure not to use short tags.
And also note that if you are embedding PHP within XML or XHTML you will need to use the <?php ?>
tags to remain compliant with standards.
Always use <?php ?>
because <? ?>
:
- will not work in coming PHP versions
- could be mixed with XML definitions. (XML always starts with
<?xml ...
) - is not enabled on many shared hosting sites.
short tags <? ?>
, only work in older php versions.
There is no difference language-wise, but many shop prefer the use of <?php
because the simple <?
opening tag can be found in XML files, which can lead to confusion for the interpreter.
Edit: I thought this was still an issue: http://terrychay.com/article/short_open_tag.shtml
精彩评论