2013-05-14 34 views
2

我知道有类似的问题,但他们无法真正回答我的问题。我正在构建一个WordPress主题,后端有很多选项。例如,我为地址制作了hcard可以包含的每个可能值的字段。我在一个查询中查询选项,然后我有很多if语句来创建我的html。当然,大多数情况下不需要。很多PHP if语句会减慢我的网站

<?php 

    $address_settings = get_option('nothing_settings_address'); ?> 

    <?php if($address_settings[ 'hcard_given_name' ] || $address_settings[ 'hcard_family_name' ]) : ?> 
     <?php $hcard_title = $address_settings[ 'hcard_given_name' ].' '.$address_settings[ 'hcard_family_name' ]; ?> 
    <?php elseif($address_settings[ 'hcard_organization' ]) : ?> 
     <?php $hcard_title = $address_settings[ 'hcard_organization' ]; ?> 
    <?php endif; ?> 

    <figure id="vcard-<?php echo $address_settings[ 'hcard_family_name' ]; ?><?php echo $address_settings[ 'hcard_given_name' ]; ?>" class="vcard one-third column" title="<?php echo $hcard_title; ?>" itemtype="http://www.data-vocabulary.org/Person/" itemscope="itemscope"> 

     <!-- Photo --> 
     <img src="<?php echo $address_settings[ 'hcard_img' ]; ?>" alt="" class="photo" itemprop="photo"> 
     <!-- alt redundant; image labeled by context, leave empty (webaim) --> 



     <!-- Caption --> 
     <figcaption> 
      <!-- Profile --> 
      <section class="profile"> 


       <!-- Name --> 
       <?php if($address_settings[ 'hcard_given_name' ] || $address_settings[ 'hcard_family_name' ] || $address_settings[ 'hcard_nickname' ]) : ?> 
       <header class="n" title="Name"> 
        <?php if($address_settings[ 'hcard_given_name' ] || $address_settings[ 'hcard_family_name' ]) : ?> 
        <span class="fn" itemprop="name"> 
         <?php if($address_settings[ 'hcard_prefix' ]) : ?> 
          <span class="honorific-prefix"><?php echo $address_settings[ 'hcard_prefix' ]; ?></span> 
         <?php endif; ?> 
         <?php if($address_settings[ 'hcard_given_name' ]) : ?> 
          <span class="given-name"><?php echo $address_settings[ 'hcard_given_name' ]; ?></span> 
         <?php endif; ?> 
         <?php if($address_settings[ 'hcard_middle_name' ]) : ?> 
          <span class="additional-name"><?php echo $address_settings[ 'hcard_middle_name' ]; ?></span> 
         <?php endif; ?> 
         <?php if($address_settings[ 'hcard_family_name' ]) : ?> 
          <span class="family-name"><?php echo $address_settings[ 'hcard_family_name' ]; ?></span> 
         <?php endif; ?> 
         <?php if($address_settings[ 'hcard_suffix' ]) : ?> 
          <span class="honorific-suffix"><?php echo $address_settings[ 'hcard_suffix' ]; ?></span> 
         <?php endif; ?> 
        </span> <!--/ .fn --> 
        <?php endif; ?> 
        <?php if($address_settings[ 'hcard_nickname' ]) : ?> 
         <span class="nickname" itemprop="nickname"><?php echo $address_settings[ 'hcard_nickname' ]; ?></span> 
        <?php endif; ?> 
       </header> <!--/ .n --> 
       <?php endif; ?> 


      <!-- Category --> 
      <?php if($address_settings[ 'hcard_cat' ]) : ?> 
      <section class="category" itemprop="category"> 
       <?php echo $address_settings[ 'hcard_cat' ]; ?> 
      </section> 
      <?php endif; ?> 


      <!-- Links --> 
      <?php if($address_settings[ 'hcard_note' ]) : ?> 
      <section class="note" itemprop="note" > 
       <?php echo $address_settings[ 'hcard_note' ]; ?> 
      </section> 
      <?php endif; ?> 


      <!-- Birthday --> 
      <?php if($address_settings[ 'hcard_bday' ]) : ?> 
      <time class="bday" itemprop="bday" > 
       <?php echo $address_settings[ 'hcard_bday' ]; ?> 
      </time> 
      <?php endif; ?> 


       <!-- Organization --> 
       <?php if($address_settings[ 'hcard_organization' ] || $address_settings[ 'hcard_unit' ] || $address_settings[ 'hcard_title' ]) : ?> 
       <div class="org" itemprop="affiliation" title="Organization" itemscope="itemscope" itemtype="http://www.data-vocabulary.org/Organization/"> 
        <?php if($address_settings[ 'hcard_organization' ]) : ?> 

        <div class="vcard"> 
         <span class="organization-name fn" itemprop="name"><?php echo $address_settings[ 'hcard_organization' ]; ?></span> 
          <?php if($address_settings[ 'hcard_orga_street' ] || $address_settings[ 'hcard_orga_city' ] || $address_settings[ 'hcard_orga_state' ] || $address_settings[ 'hcard_orga_country' ] || $address_settings[ 'hcard_orga_postal_code' ]) : ?> 
          <div class="adr" itemprop="address" itemscope="itemscope" itemtype="http://data-vocabulary.org/Address/"> 

           <!-- Address --><?php if($address_settings[ 'hcard_orga_street' ]) : ?> 
            <span class="street-address" itemprop="street-address"><?php echo $address_settings[ 'hcard_orga_street' ]; ?></span> 
           <?php endif; ?> 

           <!-- City --><?php if($address_settings[ 'hcard_orga_city' ]) : ?> 
            <span class="locality" itemprop="locality"><?php echo $address_settings[ 'hcard_orga_city' ]; ?></span>, 
           <?php endif; ?> 

           <!-- State --><?php if($address_settings[ 'hcard_orga_state' ]) : ?> 
            <?php if($address_settings[ 'hcard_orga_state' ]) : 
             $orga_state_title = $address_settings[ 'hcard_orga_state' ]; 
            else : 
             $orga_state_title = $address_settings[ 'hcard_orga_state_short' ]; 
            endif; ?> 
            <?php if(!$address_settings[ 'hcard_orga_state' ]) : 
             $orga_state = $address_settings[ 'hcard_orga_state' ]; 
            else : 
             $orga_state = $address_settings[ 'hcard_orga_state_short' ]; 
            endif; ?> 
            <abbr class="region" itemprop="region" title="<?php echo $orga_state_title; ?>"><?php echo $orga_state; ?></abbr> 
           <?php endif; ?> 

           <!-- Zip --><?php if($address_settings[ 'hcard_orga_postal_code' ]) : ?> 
            <abbr class="postal-code" itemprop="postal-code" title="<?php echo $address_settings[ 'hcard_orga_postal_code' ]; ?>"><?php echo $address_settings[ 'hcard_orga_postal_code' ]; ?></abbr> 
           <?php endif; ?> 

           <!-- Country --><?php if($address_settings[ 'hcard_orga_country' ] || $address_settings[ 'hcard_orga_country_short' ]) : ?> 
            <?php if($address_settings[ 'hcard_orga_country' ]) : 
             $orga_country_title = $address_settings[ 'hcard_orga_country' ]; 
            else : 
             $orga_country_title = $address_settings[ 'hcard_orga_country_short' ]; 
            endif; ?> 
            <?php if(!$address_settings[ 'hcard_orga_country' ]) : 
             $orga_country = $address_settings[ 'hcard_orga_country' ]; 
            else : 
             $orga_country = $address_settings[ 'hcard_orga_country_short' ]; 
            endif; ?> 
            <abbr class="country-name" itemprop="country-name" title="<?php echo $orga_country_title; ?>"><?php echo $orga_country; ?></abbr> 
           <?php endif; ?> 

           <!-- Lat/Lon (Metadata) --> 
           <!-- I will integrate that later. <span class="geo" itemprop="geo" itemscope="itemscope" itemtype="http://data-vocabulary.org/Geo/"> 
            <abbr class="latitude" itemprop="latitude" title="60.6060606060">60.6060606060</abbr> 
            <abbr class="longitude" itemprop="longitude" title="-60.6060606060">-60.6060606060</abbr> 
           </span>--> 

          </div> <!--/ .adr --> 
          <?php endif; ?> 
        </div> <!--/ .vcard --> 
        <?php endif; ?> 

        <?php if($address_settings[ 'hcard_unit' ]) : ?> 
         <span class="organization-unit"><?php echo $address_settings[ 'hcard_unit' ]; ?></span> 
        <?php endif; ?> 

        <?php if($address_settings[ 'hcard_title' ]) : ?> 
         <span class="title" itemprop="title"><?php echo $address_settings[ 'hcard_title' ]; ?></span> 
        <?php endif; ?> 

       </div> <!--/ .org --> 
       <?php endif; ?> 


       <!-- Location --> 
       <?php if($address_settings[ 'hcard_street' ] || $address_settings[ 'hcard_city' ] || $address_settings[ 'hcard_state' ] || $address_settings[ 'hcard_country' ] || $address_settings[ 'hcard_postal_code' ]) : ?> 
       <address class="adr" itemprop="address" title="Location" itemscope="itemscope" itemtype="http://data-vocabulary.org/Address/"> 

        <!-- Address --> <?php if($address_settings[ 'hcard_street' ]) : ?> 
         <span class="street-address" itemprop="street-address"><?php echo $address_settings[ 'hcard_street' ]; ?></span> 
        <?php endif; ?> 

        <!-- City --><?php if($address_settings[ 'hcard_city' ]) : ?> 
         <span class="locality" itemprop="locality"><?php echo $address_settings[ 'hcard_city' ]; ?></span>, 
        <?php endif; ?> 

        <!-- State --><?php if($address_settings[ 'hcard_state' ]) : ?> 
         <?php if($address_settings[ 'hcard_state' ]) : 
          $state_title = $address_settings[ 'hcard_state' ]; 
         else : 
          $state_title = $address_settings[ 'hcard_state_short' ]; 
         endif; ?> 
         <?php if(!$address_settings[ 'hcard_state' ]) : 
          $state = $address_settings[ 'hcard_state' ]; 
         else : 
          $state = $address_settings[ 'hcard_state_short' ]; 
         endif; ?> 
         <abbr class="region" itemprop="region" title="<?php echo $state_title; ?>"><?php echo $state; ?></abbr> 
        <?php endif; ?> 

        <!-- Zip --><?php if($address_settings[ 'hcard_postal_code' ]) : ?> 
         <abbr class="postal-code" itemprop="postal-code" title="<?php echo $address_settings[ 'hcard_postal_code' ]; ?>"><?php echo $address_settings[ 'hcard_postal_code' ]; ?></abbr> 
        <?php endif; ?> 

        <!-- Country --><?php if($address_settings[ 'hcard_country' ] || $address_settings[ 'hcard_country_short' ]) : ?> 
         <?php if($address_settings[ 'hcard_country' ]) : 
          $country_title = $address_settings[ 'hcard_country' ]; 
         else : 
          $country_title = $address_settings[ 'hcard_country_short' ]; 
         endif; ?> 
         <?php if(!$address_settings[ 'hcard_country' ]) : 
          $country = $address_settings[ 'hcard_country' ]; 
         else : 
          $country = $address_settings[ 'hcard_country_short' ]; 
         endif; ?> 
         <abbr class="country-name" itemprop="country-name" title="<?php echo $country_title; ?>"><?php echo $country; ?></abbr> 
        <?php endif; ?> 

        <!-- Lat/Lon (Metadata) --> 
        <!-- I will integrate that later. <span class="geo" itemprop="geo" itemscope="itemscope" itemtype="http://data-vocabulary.org/Geo/"> 
         <abbr class="latitude" itemprop="latitude" title="30.3030303030">30.3030303030</abbr> 
         <abbr class="longitude" itemprop="longitude" title="-30.3030303030">-30.30303030</abbr> 
        </span>--> 

       </address> <!--/ .adr --> 
       <?php endif; ?> 


       <!-- Contact --> 
       <?php if($address_settings[ 'hcard_url' ]) : ?> 
        <a class="url" href="<?php echo $address_settings[ 'hcard_url' ]; ?>"><?php echo $address_settings[ 'hcard_url' ]; ?></a>, 
       <?php endif; ?> 
       <?php if($address_settings[ 'hcard_postal_code' ]) : ?> 
        <a href="mailto:<?php echo $address_settings[ 'hcard_email' ]; ?>" class="email"><?php echo $address_settings[ 'hcard_email' ]; ?></a> 
       <?php endif; ?> 

       <?php if($address_settings[ 'hcard_work' ] || $address_settings[ 'hcard_private' ] || $address_settings[ 'hcard_mobile' ] || $address_settings[ 'hcard_fax' ]) : ?> 
       <ul itemscope="itemscope" itemtype="http://www.data-vocabulary.org/Organization/"> 
        <?php if($address_settings[ 'hcard_private' ]) : ?> 
         <li class="tel"><abbr class="type" title="home pref">Home:</abbr> <abbr class="value" itemprop="tel" title="<?php echo $address_settings[ 'hcard_private' ]; ?>"><a href="tel:<?php echo $address_settings[ 'hcard_private' ]; ?>"><?php echo $address_settings[ 'hcard_private' ]; ?></a></abbr></li> 
        <?php endif; ?> 
        <?php if($address_settings[ 'hcard_work' ]) : ?> 
         <li class="tel"><abbr class="type" title="work">Office:</abbr> <abbr class="value" itemprop="tel" title="<?php echo $address_settings[ 'hcard_work' ]; ?>"><a href="tel:<?php echo $address_settings[ 'hcard_work' ]; ?>"><?php echo $address_settings[ 'hcard_work' ]; ?></a></abbr></li> 
        <?php endif; ?> 
        <?php if($address_settings[ 'hcard_mobile' ]) : ?> 
         <li class="tel"><abbr class="type" title="mobile">Mobile:</abbr> <abbr class="value" itemprop="tel" title="<?php echo $address_settings[ 'hcard_mobile' ]; ?>"><a href="tel:<?php echo $address_settings[ 'hcard_mobile' ]; ?>"><?php echo $address_settings[ 'hcard_mobile' ]; ?></a></abbr></li> 
        <?php endif; ?> 
        <?php if($address_settings[ 'hcard_fax' ]) : ?> 
         <li class="tel"><abbr class="type" title="fax">Fax:</abbr> <abbr class="value" itemprop="tel" title="<?php echo $address_settings[ 'hcard_fax' ]; ?>"><a href="tel:<?php echo $address_settings[ 'hcard_fax' ]; ?>"><?php echo $address_settings[ 'hcard_fax' ]; ?></a></abbr></li> 
        <?php endif; ?> 
       </ul> 
       <?php endif; ?> 
      </section> <!--/ .profile --> 
     </figcaption> <!--/ .figcaption --> 
    </figure> 

我想我的主题非常快。没有它可能的那么快。但速度足以被称为“快速主题”。我在我的主题中做这样的事情。所有这些陈述都可以减慢速度吗?

+3

if语句不会减慢网站下来感知,它的循环和数据库/文件系统访问减慢脚本 – 2013-05-14 08:11:50

+1

我不知道workpress ..你必须经常启动,停止PHP?你可以使用开关\案例 – 2013-05-14 08:14:15

回答

4

“如果/那么”的声明本身通常不会是一个很大的性能打击,但从我的经验踢在PHP本身可能会导致一点点击中。我将使用PHP的更大的块,并使用回声的HTML,像这样重新写:

相反的:

<?php if ($this > $that) { echo 'this'; } ?> 
<p>This is not that!</p> 
<?php if ($other < $every) { echo 'is'; } ?> 
<p>This is not efficient.</p> 
/* and so forth... */ 

你会想这样做:

<?php 
    if ($this > $that) { echo 'this'; } 
    echo '<p>This is not that!</p>'; 
    if ($other < $every) { echo 'is'; } 
    echo '<p>This is not efficient.</p>'; 
    /* and so forth... */ 
?> 

编辑(如通过MildlyInteresting提到的)甚至更有效的方法:

<?php 
    var $output = ''; 

    if ($this > $that) { 
    $output .= 'this'; 
    } 

    $output .= '<p>This is not that!</p>'; 

    if ($other < $every) { 
    $output .= 'is'; 
    } 

    $output .= '<p>This is not efficient.</p>'; 
    /* and so forth... */ 

    echo $output; 
?> 

这不是最好的例子,但我认为你明白了。

卢克

+1

也许值得一提的是,附加到输出字符串,在最后打印一次比多个单独的字符串回显效果要好很多倍。 – MildlySerious 2013-05-14 08:19:19

+0

@MildlyInteresting绝对。更新后。干杯。 – 2013-05-14 08:23:00

+0

我只是这么做的,因为很多人这样做,我不知道这是否是良好的做法下WordPress的坏的做法,我只是觉得它可读。 – 2013-05-14 08:27:57