Jump to content

PHP coding best practice

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
4 replies to this topic

#1
ductiletoaster

ductiletoaster

    Newbie

  • Members
  • PipPip
  • 15 posts
Ok so im developing a wordpress theme for a client and i was organize some of my code and i realized that i was implementing two different was of creating html from php. I just wanted to know was there a difference between the two methods i employee... ie Efficiency... correctness.. so on!

METHOD ONE:

function logo_main() { ?>
      <a href="index.php"><img class="main_logo" src="wp-content/themes/gopartybuzz/library/images/logo.png" alt="Go Party Buzz" /></a>
    <ul id="category_menu">
        <?php wp_list_categories('orderby=name&title_li=&depth=1&show_count=0&use_desc_for_title=1&child_of='.NULL); // Null is used to keep from showing subcats ?> 
    </ul>

    <div id="searchform">
           <form id="searchform" method="get" action="<?php bloginfo('home') ?>">
            <div>
                <input id="s" class="textInput" name="s" type="text" value="Search..." onfocus="if (this.value == 'Search...') {this.value = '';}" onblur="if (this.value == '') {this.value = 'Search...';}" size="28" />
            </div>
        </form>
    </div><!--#searchform-->
<?php }


METHOD TWO:

function childtheme_create_doctype($content) {
    $content = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">';
    $content .= "\n";
    $content .= '<html xmlns="http://www.w3.org/1999/xhtml"';
    return $content;
}


Keep in mind these are two different functions that have different outputs but what im trying to illustrate is the difference int he style of functions one uses a variable to hold data and then returns it for output while the other simply has the proper HTML in it... So which is more proper?Efficient? Faster?

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
I prefer Method 1, myself, but use Method 2 if I'm in the middle of a lot of dynamically generated code.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
ductiletoaster

ductiletoaster

    Newbie

  • Members
  • PipPip
  • 15 posts
yeha i agree though im still wondering if the second method is less efficient. or if it matters. Now of course this is is only opne function but i like to stay consistent and at the end of this project there will be hundreds of lines if not thousands of lines of code and so even a small difference in efficiency makes a difference. But i think you are right. It makes more sense to employee method two in a more dynamic setting.

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,124 posts
There's one rule for choosing which one you should use, and that is Don't. The first one is more efficient as it closes the delimiter (thus allowing the interpreter to skip the block of code all together), but the second method has to parse the text block as you're not using single quotes on it. If you're going to choose a microoptimization in PHP, then you've made a poor choice as PHP is already an unefficient language in many aspects, the two codes will act similarily within the interpter hashtables so it does not really matter unless you're wasting function space, both styles are used. Consistency should be the only factor in your choice.

#5
ductiletoaster

ductiletoaster

    Newbie

  • Members
  • PipPip
  • 15 posts
Well that answered my question! haha... Now though i agree that PHP is inefficient in many aspects I warn others to not disregard it. Most languages including server-side scripting languages such as java, php, asp etc have the flaws and on top of these flaws the servers they are running off of many times may not be configured correctly. There are many many great open source projects that employee PHP and it would be wise to learn as much about each language as u can. Because what maybe right for one situation may be wrong for another!

But thank u Nullw0rm u answered my question and just so u know the reason im using PHP in this case is because im building a wordpress theme... which is in PHP!

HAHA well after thinking about it i decided to look it up and i found a JAVA project of Wordpress! Crazy