Question

I know that there are similar questions, but they couldn't really answer mine. I'm building a wordpress theme and there are lots of options in the backend. For example for the address I made fields for every possible value an hcard can contain. I query the options in one single query, then I have lots of if-statements to create my html. Of course most of it isn't needed in most cases.

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

I want my theme to be very fast. Not as fast as it possibly could be. But fast enough to be called a "fast theme". I do stuff like this all around my theme. Can all these statements slow it down?

Was it helpful?

Solution

The "if/then" statements themselves generally won't be a big performance hit, however from my experience kicking in PHP itself can cause a bit of a hit. I would re-write using bigger blocks of PHP and using echos for HTML, like so:

Instead of:

<?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... */

You'd want to do this:

<?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... */
?>

EDIT An even more efficient approach (as mentioned by 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;
?>

This is not the best example, but I think you get the idea.

Luke

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top