Domanda

I'm currently following this basic Codeigniter tutorial and the author used this similar code in this view/post_index.php page

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

<?php
    }
}
?>

and I get an empty page with this

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

in my source.

However when I use this

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

I'm fine. It shows all my posts. I'm running off wamp (just downloaded the 64bits & Apache 2.4, 2.2E Version off the website, except I don't see much difference with all the other 4 packages they have....) with Apache Version : 2.2.21
PHP Version : 5.3.10

What is going on?

Thank you.

È stato utile?

Soluzione

That normally means you don't have short-tags enabled (most PHP < 5.4.0 installations turn them of by default). You'll need to check your php.ini file.

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

Altri suggerimenti

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.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top