嵌套的替代if语句在视图中混合php和html时不起作用[重复]

This question already has an answer here:

I migrate my old app. On old server (php 5.2) I'm using nested alternative if statement to mix html and php code in view, like this:

<?php if($pg_id==1): ?>
    <li <?php if ($nav2=='news'): echo 'class="active"'; endif; ?>><a href="<?php echo base_url();?>cms/news/add">News</a></li>
<? endif; ?>

Why on the new server (php 5.5) the above example doesn't works? I'm getting error:

Parse error: syntax error, unexpected end of file in C:\xampp\htdocs\testapp\system\libraries\Loader.php(673) : eval()'d code on line 372

If I want this statement works, I need to rewrite it like this:

<?php if($pg_id==1):
    echo '<li';
    if ($nav2=='news')
        echo 'class="active"';
    echo '><a href="' . base_url() .'cms/news/add">News</a></li>';
endif; ?>

The app is written using Codeigniter 1.7.2.

</div>

Try it it will works... If not comment please

<?php if($pg_id==1):
    if ($nav2=='news'):?>
   <li class="active"><a href="<?php echo base_url('cms/news/add');?>">News</a> </li>
   <?php else: ?>
   <li class=""><a href="<?php echo base_url('your path'); ?>">Title</a></li>
 <?php endif;
  endif;
 ?>

Rather than using above code use the Ternary Operator. It will be much easy and understandable.

<?php  
$class= ($nav2=='news')?'active':'';
  if($pg_id==1): ?>
  <li class="<?php echo $class;?>"><a href="<?php echo base_url('cms/news/add');?>">News</a></li>
<?php endif; ?>

Try it.. will be 100% works.