1

可能重复:
是否可以使用 PHP 短标签?

<?php
    //Some code
?>

或者

<?
    //Some code
?>

我知道第一种方法是正确的方法,但 PHP 代码没有经过验证。那么,除了节省额外的输入和字节之外,这有关系吗?

更新 感谢回复...

  1. 我不知道它们被称为短标签(因此我没有找到重复的 SO 问题)
  2. 我不知道有一个特定的服务器配置选项来允许/禁止短标签。

再次感谢


您可以将所有值放入一个数组中,然后对其进行序列化:

$string = serialize(array(1, 2, 'foo', 'tree', 'monkey');

这将为您提供一个存储在数据库中的字符串。稍后,您可以通过反序列化来恢复您的阵列:

$array = unserialize($string);
4

6 回答 6

10

It does, if anyone ever uses your code on a server where short tags are disabled. I work with several servers where they are. Other than that though, no. Using the short version makes your script less portable though for the above mentioned reason. This may or may not be an issue for you.

This is another issue entirely, but related. If you are trying to generate certain types of files from PHP (XML is the candidate that comes up most often for me) then having short tags can be an issue. For instance, the following causes a PHP syntax error:

<?xml version="1.0" ?>

You must instead write the following on a server that has short tags enabled:

<?php echo '<?xml version "1.0" ?>'; ?>

Gah!

于 2009-10-20T06:52:34.523 回答
4

If your project is likely to be deployed on different servers (open source software, for example) it is best to always use <?php

However, if you're like me, and always strive for maximum portability, use <?php even if you don't believe your software will ever leave your server. Most servers have short tags enabled.

However, if they have short tags disabled, and you use them, your PHP will be exposed to the world (if under the document root).

于 2009-10-20T06:54:48.440 回答
0

No difference it's just a matter of preference I think, plus why should it matter? it's just another 3 bytes.

Edit:

Forgot to say that you have to enable short hand in php.ini

于 2009-10-20T06:52:35.757 回答
0

the closing bracket is not compulsory

于 2009-10-20T06:53:30.917 回答
0

On some systems, the default option for the short_open_tags is off, so the latter doesn't work, while the former does, so it can completely break your website if you use the second. Personally, I just like to override the setting and use the second.

于 2009-10-20T06:54:02.040 回答
0
<? ?>

是短标签,如果短标签关闭,它将不起作用。

于 2009-10-20T08:05:52.863 回答