使多个PHP if语句更有效

时间:2013-06-29 14:28:22

标签: php wordpress

我开始使用PHP获取WordPress,并编写了一些代码,将一些社交网络图标放在页脚中。我完成它的方式有效,我只是调用存储在数据库中的社交网络URL的内容,如果页脚中有图标/链接。但对我来说看起来效率很低,这里是代码,有没有人知道如何提高效率。

      <?php 
        $social1 = of_get_option('fab_social_twitter_url');
        $social2 = of_get_option('fab_social_facebook_url');
        $social3 = of_get_option('fab_social_linkedin_url');            
      ?>



      <!-- divs for right social network icons column -->
      <div class="eight columns">
        <div class="social">
          <ul>

            <?php
                if(!empty($social1)) {
            ?>
                <li><a href="<?php echo of_get_option('fab_social_twitter_url'); ?>"><img src="<?php echo of_get_option('fab_social_twitter_icon'); ?>" alt="Follow us on Twitter"></a></li>
            <?php
             } 
            ?>

            <?php
                if(!empty($social2)) {
            ?>
                <li><a href="<?php echo of_get_option('fab_social_facebook_url'); ?>"><img src="<?php echo of_get_option('fab_social_facebook_icon'); ?>" alt="Follow us on Facebook"></a></li>
            <?php
             } 
            ?>            

            <?php
                if(!empty($social3)) {
            ?>                  
                <li><a href="<?php echo of_get_option('fab_social_linkedin_url'); ?>"><img src="<?php echo of_get_option('fab_social_linkedin_icon'); ?>" alt="Follow us on Linkedin"></a></li>
            <?php
             } 
            ?>            

          </ul>
        </div>
      </div>

3 个答案:

答案 0 :(得分:2)

也许:

  <!-- divs for right social network icons column -->
  <div class="eight columns">
    <div class="social">
      <ul>
        <?php
            foreach (array("twitter","facebook","linkedin") as $option)
                ($tmp=of_get_option('fab_social_'.$option.'_url')) && (print('<li><a href="'.$tmp.'"><img src="'.of_get_option('fab_social_'.$option.'_icon').'" alt="Follow us on '.ucfirst($option).'"></a></li>'));
        ?>
      </ul>
    </div>
  </div>

答案 1 :(得分:1)

从性能的角度来看,我认为这里没有任何优化,三个单独的案例单独测试

答案 2 :(得分:0)

  <?php 
    $social1 = of_get_option('fab_social_twitter_url');
    $social2 = of_get_option('fab_social_facebook_url');
    $social3 = of_get_option('fab_social_linkedin_url'); 

    $icon1 = of_get_option('fab_social_twitter_icon');
    $icon2 = of_get_option('fab_social_facebook_icon'); 
    $icon3 = of_get_option('fab_social_linkedin_icon');          
  ?>



  <!-- divs for right social network icons column -->
  <div class="eight columns">
    <div class="social">
      <ul>

        <?php if(!empty($social1)) { ?>
            <li>
               <a href="<?php echo $social1; ?>">
                  <img src="<?php echo $icon1; ?>" alt="Follow us on Twitter">
               </a>
            </li>
        <?php } ?>

        <?php if(!empty($social2)) { ?>
            <li>
               <a href="<?php echo $social2; ?>">
                  <img src="<?php echo $icon2; ?>" alt="Follow us on Facebook">
               </a>
            </li>
        <?php } ?>

        <?php if(!empty($social3)) { ?>
            <li>
               <a href="<?php echo $social3; ?>">
                  <img src="<?php echo $icon3; ?>" alt="Follow us on Linkedin">
               </a>
            </li>
        <?php } ?>   

      </ul>
    </div>
  </div>