ELSE and I don't know why

Here's my php code:

<?php if($user->uid == '1'){ ?>
    <h3 class="info">Upcoming Games</h3>    <!--MY CHANGE -from Athletics Events -->
    <?php } ?>
    <?php else { ?> <h3 class="info">Athletic Events</h3>     <?php }?>

Why do I get this error? I have all the brackets I need, don't I?


The } and else { can't be broken apart with PHP tags the way that you have it. Think of it as if you were trying to do:

<?php
if($some_condition) {
    //do something
}
echo '     ';
else {
    //something else
}

This would give you a parse error. Because you are closing the PHP tags, and then effectively outputting whitespace, then reopening, your code is behaving similarly to this. The same also applies if you were to be doing <?php }?><?php else {?> as well, only it would behave like you were doing echo ''; in between.


更清洁,更不容易出错的方法是使用:

<?php if($user->uid == '1'): ?>
<h3 class="info">Upcoming Games</h3>    <!--MY CHANGE -from Athletics Events -->

<?php else: ?>
<h3 class="info">Athletic Events</h3>     

<?php endif; ?>

像这样尝试

<?php if($user->uid == '1'){ ?>
    <h3 class="info">Upcoming Games</h3>    <!--MY CHANGE -from Athletics Events -->
    <?php } else { ?> <h3 class="info">Athletic Events</h3>     <?php }?>
链接地址: http://www.djcxy.com/p/12024.html

上一篇: 意外的T

下一篇: ELSE,我不知道为什么