Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Parse error: syntax error, unexpected T_ELSE and I don't know why

Tags:

php

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?

like image 644
user1015214 Avatar asked Mar 06 '26 08:03

user1015214


2 Answers

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.

like image 193
jprofitt Avatar answered Mar 07 '26 21:03

jprofitt


A cleaner and less error prone way is to use :

<?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; ?>
like image 40
Andreas Wong Avatar answered Mar 07 '26 20:03

Andreas Wong



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!