Warning: array_merge() [function.array-merge]: Argument #1 is not an array in /home/jackbor/public_html/blog/wp-content/plugins/headmeta.php on line 52

Warning: array_merge() [function.array-merge]: Argument #2 is not an array in /home/jackbor/public_html/blog/wp-content/plugins/headmeta.php on line 52

Quick Tip For PHP Loops

When you’re writing lengthy code it’s really easy to get confused and lose track of your control loops.

When I was making the transition from PHP newbie to being able to write my own code, I often had trouble keeping track of all the if/then loops, foreach loops, and all the other various control procedures. Invariably I’d type some code, upload it to the server, hit it with my browser only to get no output and no error code… the worst possible result.

That horrible blank screen that means a trip back to Notepad to hunt through lines and lines of code to figure out if it’s a missing } or one too many } or some other silly typo.

Many years later, and many many projects the wiser, I’ve got a coding method that all but eliminates these kind of time wasting coding problems.

It’s really quite simple… a common characteristic of great solutions.

Whenever I come to a part in my code that calls for a control loop such as

  • if/then
  • switch
  • foreach
  • while
  • etc…

I put in all the brackets first and then go back and fill in the logic.

I think an example will clarify this completely. Imagine you’re watching over my shoulder as I am coding some script at 2:00 AM (when I do some of my best work, unfortunately).

if($x == '1')
{

}
elseif($x == '2')
{

}
else
{

}

Now, the sample code above could be written using switch() or in several ways that are more efficient and just flat out better. But the point I’m illustrating is that I get all of the brackets written out first and then go back in and fill in the code.

An extra step that has proven useful is to comment the end of the brackets before filling in the code. So, in my example you’d see me do this:

if($x == '1')
{

}//end of x==1
elseif($x == '2')
{

}//end of x==2
else
{

}//end of else x isn't 1 or 2

Now that my control loops are written out and I’m confident that the code will run without any errors, I can go back in and write the code that is executed for each step of the logic.

And if I loose track of where I am in the code, my comments will help me back on track.

I can’t tell you how much more efficient this method is for me. I haven’t seen it explained elsewere so I wanted to share it with you. I hope it helps eliminate hours of needless frustration with your next coding project.

2 Responses to “ Quick Tip For PHP Loops”

  1. Angalee says:

    I’ve just started teaching myself PHP. Thank you for the tips, I’m sure I’ll be needing them.

  2. Tim says:

    I’ve been programming with PHP over 5 years and everything you say in this article is true.

    I would add a couple more things:

    1. Use an editor that can find the opposite brace. I use EditPlus (www.editplus.com) and I can press ctrl-] to find the other side of a code block.

    2. Usually (there are always exceptions) if your code block spans more than one screen vertically it could be broken up into functions to make it easier to read, and to promote code re-use.

    As for the approach of placing the punctuation of the code block first, that is something I have only this year started doing and it is a huge help.

    Thanks for a nice article.

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>

Calendar

June 2005
M T W T F S S
    Jan »
 12345
6789101112
13141516171819
20212223242526
27282930