user1015214
user1015214

Reputation: 3081

Parse error: syntax error, unexpected T_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?

Upvotes: 2

Views: 1523

Answers (4)

codingbiz
codingbiz

Reputation: 26396

Rewrite like this and let's see

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

this also worked

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

that was a replica of your code but the else is move up. So it is obvious else should not be at the start

Upvotes: 2

Andreas Wong
Andreas Wong

Reputation: 60584

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

Upvotes: 4

jprofitt
jprofitt

Reputation: 10964

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.

Upvotes: 6

Kris
Kris

Reputation: 6122

Try it like this

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

Upvotes: 2

Related Questions