可以使用很多PHP if语句减慢我的网站速度

时间:2013-05-14 08:10:40

标签: php performance options statements

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

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

我希望我的主题非常快。没有尽可能快。但足够快,被称为“快速主题”。我在我的主题周围做这样的事情。所有这些陈述都可以减慢它吗?

1 个答案:

答案 0 :(得分:4)

“if / then”语句本身通常不会有很大的性能影响,但是根据我的经验,踢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;
?>

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