Make multiple PHP if statements more efficient

2019-09-16 20:05发布

I am starting out with PHP for a WordPress and have written some code to put some social network icons in the footer. The way I have done it works, I'm simply calling the content of social network URL stored in the DB and if there is something the icon/link in the footer. But looks very inefficient to me, here is the code, does anyone know how to make it more efficient.

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

标签: php wordpress
3条回答
Explosion°爆炸
2楼-- · 2019-09-16 20:22

From a performance point of view, I don't think that there is anything to optimize in here, three separate cases tested individually

查看更多
迷人小祖宗
3楼-- · 2019-09-16 20:24
  <?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>
查看更多
Lonely孤独者°
4楼-- · 2019-09-16 20:26

Maybe:

  <!-- 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>
查看更多
登录 后发表回答