1

我目前正在关注这个基本的 Codeigniter 教程,作者在这个 view/post_index.php 页面中使用了这个类似的代码

if (!isset($posts)){ ?>
    <p> No Post to display </p>
<?php
} else {
    foreach ($posts as $row){
?>
    <h2> <?=$row['title']?> </h2>
        <p> <?=$row['post'] ?></p>

<?php
    }
}
?>

我得到一个空白页面

<h2> <?=$row['title']?> </h2>
<p> <?=$row['post'] ?></p>

在我的来源。

但是,当我使用这个

    <h2> <?php echo $row['title']?> </h2>
    <p> <?php echo $row['post'] ?></p>

我很好。它显示了我所有的帖子。我正在运行 wamp(刚刚从网站上下载了 64 位和 Apache 2.4、2.2E 版本,除了我看不出与他们拥有的所有其他 4 个软件包有太大区别......)与 Apache 版本:2.2.21
PHP版本:5.3.10

到底是怎么回事?

谢谢你。

4

2 回答 2

3

这通常意味着您没有启用短标签(大多数 PHP < 5.4.0 安装默认关闭它们)。你需要检查你的php.ini文件。

Before you enable them, I would suggest reading https://softwareengineering.stackexchange.com/questions/151661/is-it-bad-practice-to-use-tag-in-php - there are pros and cons to using the short-tag syntax.

@IMSoP also makes a very valid note:

Specifically, you need the short_open_tag option (http://www.php.net/manual/en/ini.core.php#ini.short-open-tag) enabled if using PHP < 5.4. From 5.4 onwards, this setting is still present, but is not needed for the <?= ... ?> case, only the potentially problematic <? ... ?>

于 2013-04-07T21:34:18.580 回答
-2

if you want to use short_tags, you NEED short_tags, you have to enable them in application/config/config.php

$config['rewrite_short_tags'] = TRUE;`

check the doc please Codeigniter Documentation

the documentation clearly says:

Note: If you find that the syntax described in this page does not work on your server it might be that "short tags" are disabled in your PHP ini file. CodeIgniter will optionally rewrite short tags on-the-fly, allowing you to use that syntax even if your server doesn't support it. This feature can be enabled in your config/config.php file.

Please note that if you do use this feature, if PHP errors are encountered in your view files, the error message and line number will not be accurately shown. Instead, all errors will be shown as eval() errors.

于 2013-04-07T21:48:39.833 回答