A Few Tips for Speeding Up PHP Code

Discussion in 'PHP' started by Sanskruti, Feb 26, 2007.

  1. Sanskruti

    Sanskruti New Member

    Joined:
    Jan 7, 2007
    Messages:
    108
    Likes Received:
    18
    Trophy Points:
    0
    Occupation:
    Software Consultant
    Location:
    Mumbai, India
    All like to create PHP scripts as fast as possible, and we try to take any shortcuts that we have.

    Here are few tips to speed up php code

    Tip -1



    Using a good editor can really save you time. If you're still stuck on using Notepad to edit your PHP scripts, switch right now. There are plenty of free alternatives around, such as PHPEdit or EditPlus 2 .The biggest advantage you get with a proper editor is code highlighting. Code highlighting can help you debug your scripts, or follow code logic. Most editors support code highlighting, and if yours doesn't, then it's not suited for coding. Using a good editor is the ability to do powerful search-and-replace operations. EditPlus 2 supports regular expressions in their search-and-replace functionality, and most editors do. It just makes it so much easier to replace a snippet of code. You can also use a full-blown PHP IDE (Integrated Development Environment).. An IDE can really save you time, by making the debugging process easier. In short, use a good PHP editor or IDE, and you will save a very big amount of time

    Tip - 2



    Most of your PHP scripts will probably do many of the same tasks, such as database functionality (insert, update, select, delete), and have many of the same functions. You could re-write these functions and tasks every time you start a new project, but that seems a bit pointless. Instead, you could use an existing framework or skeleton, and base your new scripts on that. You can either use a PHP framework, like CakePHP or the Seagull Framework, but you can also use a very simple skeleton that you create yourself. It doesn't have to be complicated at all, and can even be just one file which contains all the functions you commonly use. In short, you should try to re-use as much code as you can, and try to create some short of skeleton or framework to really speed up development.

    Tip -3



    If you're going to build a CMS for your website, which is actually one of the most common things PHP developers create, then you should first look at existing scripts. There are plenty of great scripts already, and many of them are completely free, often licensed under an open source license, such as the GPL license. A good example of this is WordPress. Originally, WordPress is a blogging tool, but it can easily be used as a CMS for your website, and you can even extend it using template hacks, plugins or code modifications. This will save you a significant amount of time, which means you can focus your time on other tasks. Of course in some situations, when you have really specific needs, it won't be possible to use an existing script.

    Tip - 4



    During the development of a new script, you will probably run across many problems, and you'll have to think of a good solution. The best way is to keep the solution as simple as possible, and as soon as you notice that your code becomes complicated, you should consider re-doing it in another way. The best way is to make everything as simple as possible. Keep everything as short as possible, and if-statements and loops should be never longer than 10-20 lines. If they become longer, first look if you can do it simpler, and if not possible then break it up in blocks, to make it easier to follow. In some cases rewrite a particular block of code 3 or 4 times. This might seem a huge waste of time, but it will save you a huge amount of time in the long run. So in short, try to keep everything as simple as possible. You should be able to immediately understand what's happening.

    Tip -5



    Another great way to speed up development is to properly document your code, especially the complicated parts

    PHP:
    <?php 
    // Opening connection
    $linkid mysql_connect ('localhost''sa''pass');
    // Selecting database
    mysql_select_db('mydb'$linkid);
    // Running a query
    $query mysql_query ("SELECT * FROM table");
    // etc...
    ?>
    As you can see, the comments add no value at all. Just by quickly glancing, you can figure it everything the comments say, so the comments don't serve any purpose at all. Better would be something like this:
    PHP:
    <?php 
    // Get our content from database
    $linkid mysql_connect ('localhost''sa''pass');
    mysql_select_db('mydb'$linkid);
    $query mysql_query ("SELECT * FROM table");
    // Below is the code for our search algorithm
    // This algorithm first looks for a certain keyword
    // and the orders it on relevancy
    // ... code for algorithm goes here ...
    // Now we must make sure that all the posts
    // are filtered through the sensor.
    // This sensor checks each post, and looks for bad stuff 
    // ... code for sensor check ...
    ?>
    This is already much better, because we immediately know what the purpose is a of certain code block. From there we can then figure out how the code actually works.

    Even this style of commenting isn't perfect, but there isn't a "perfect" style of commenting. Each developer has his or her own style, but make sure that other developers can read your code as well. This is especially important when you work in a team.

    If you properly document your code, you will be able to understand it much easier later on, which means you won't have to spend time on figuring out what the code actually does, and can immediately start writing new code. Good documentation can really save you a good deal of time.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice