如何优化php语法

时间:2013-10-13 21:08:23

标签: php if-statement

下面的PHP调用工作正常,但也许还有另一种更清晰的方式来显示它?

<?php if ($this['modules']->count('top-a')) : ?>
       <?php if ($this['config']->get('warp_onlyhome')) : ?>
          <?php $catid = JRequest::getInt('catid'); if ($catid == 0) : ?>
    <section id="top-a" class="grid-block"><?php echo $this['modules']->render('top-a', array('layout'=>$this['config']->get('top-a'))); ?></section>
          <?php endif ?>
          <?php else: ?>
    <section id="top-a" class="grid-block"><?php echo $this['modules']->render('top-a', array('layout'=>$this['config']->get('top-a'))); ?></section>
        <?php endif; ?>
     <?php endif; ?>

2 个答案:

答案 0 :(得分:1)

这是如何使用if else嵌入的示例。

<? if ($condition): ?>
  <p>Content</p>
<? elseif ($other_condition): ?>
  <p>Other Content</p>
<? else: ?>
  <p>Default Content</p>
<? endif; ?>

请参阅more examplealternative

答案 1 :(得分:1)

您使用else:

<?php if ($this['config']->get('warp_onlyhome')) : ?>
   <?php $catid = JRequest::getInt('catid'); if ($catid == 0) : ?>

my content goes here

   <?php endif: ?>
   <?php else: ?>

my content 2

<?php endif; ?>

较少疯狂的格式:

if ($this['config']->get('warp_onlyhome')) {
   $catid = JRequest::getInt('catid'); 
   if ($catid == 0) {
      echo "my content goes here";
   }
} else {

  echo "my content 2";

}