Postcards From My Life

Lint I find in my mind's belly-button.
  • EPK
  • Consulting
  • Resume
  • Nerd Herding
  • Talks
  • CWJ 09
« Forks!=Threads!
Building a “Tag Cloud” in PHP »

How NOT to write a FOR loop in PHP

Dear Reader,

Ok, so I’m on a programming roll right now…sue me.

Here’s a piece of code I saw yesterday on a mailing list. At first I thought it was borked and started to reply that there’s no way it would work…son-of-a-gun if it doesn’t…sort of.

< ?php
for ($j=A; $j <= Z; $j++)
{
    echo $j."\n";
    if ($j==Z) break;
}
?>

I’ve never seen anyone increment a character value before. It intrigued me so I played with it for a few minutes. First let me say if someone on one of my teams every wrote code like this…well, it’s a ‘Career Limiting Move’. Character values are just that, characters. If you need to build a list of the letters of the alphabet, do it the way programmers have been doing it for at least the 23 years I’ve been programming, iterate through the numbers 65-91. Use the chr() function to get your letter. (c’mon, other than the numbers changing, we even did that on my old C=64!)

The second problem with this code is that without the IF statement, that it doesn’t do exactly what you would expect it to do. Run the code without the IF and look at the output. I’m not sure I know why it does that. I’m not sure why $j does not evaluate to == Z when it actually displays the Z. I could probably dig in and find it but I really don’t care.

For those interested, a better way to do this would be:

< ?php
for ($j=0; $j <26; $j++)
{
  echo chr(65+$j)."\n";
}
?>

It’s fewer lines of code and we are not using string types as numbers.

This is a good example of “Just because you can, doesn’t mean you should.”

Until next time, my prayers are with you today.
(l)(k)(bunny)
=C=

Related posts

  • No related posts.

This entry was posted on Saturday, December 3rd, 2005 at 12:50 pm and is filed under PHP, Programming. You can follow any responses to this entry through the RSS 2.0 feed.Both comments and pings are currently closed.

Comments are closed.

  • Team Based PHP Training

  • Sponsors and Ads

  • Conferences I’m Attending

  • About Me

    cal_evansThis is my blog. Sometimes it's my deep thoughts, sometimes it's a journal of things I've learned. Every now and then it's my box of shattered dreams. Most of the time though, it's just the place I like to write. Sit with me as I show you some postcards from my life. While you are here, do me a favor and leave a comment.

    If you are looking for my contact information, bio, picture, ASL, check out my EPK.

    My name is Cal Evans and this is my blog.



    Follow me on FriendFeed!

    View Cal Evans's profile on LinkedIn

  • My First Book

  • Support PHPWomen


    US Shop | European Shop

  • What I'm Doing...

    • I'm at Mediterranean Cuisine (400 21st Avenue South, grand ave, Nashville). 1 min ago
    • @dragonmantank Great! Now no lurking. :) Start talking. (Start a discussion if you don't like the current ones.) in reply to dragonmantank 49 mins ago
    • @dragonmantank PHP Community See if this link works http://bit.ly/b0CRBi in reply to dragonmantank 54 mins ago
    • More updates...

  • Tags

    API article Cal Evans codeworks conference contest cw09 developers devzone elizabeth naramore Exim flex fun IBuildings iPod jobs Kathy Evans linkedin Management Marketing microsoft MySQL Nashville phar photography PHP phparchitect php developers podcampnashville podcast podcasting poem Programming Quickies respect Silly-Con Valley sixty second tech software development terry chay twitter upgrade video wordpress zend zend framework

  • RSS PHP Podcasts

    • oddWeek Episode #4
    • Creating Custom Zend_Form Decorators
    • Habits of Highly Scalable Web Applications
    • PHPSPCast #6 – Ao vivo da Campus Party (Q&A)
    • php|architect Podcast: oddWeek #002
    • Podcast #2010-02: Stalker Edition
    • php|architect podcast: oddWeek #003
    • php|architect Podcast: oddWeek #001
    • php|architect Podcast: 2009 in Revue
    • php|architect Podcast: Bake’n’Shake

  • XBox Gamer Card

  • Me

    • Best web design company
    • Cal Evans Dot Com
    • Cyrano’s Apprentice
    • Evans Internet Construction Company
    • My Life as a Child
    • PHP Podcasts
    • Sixty Second Tech

  • RSS My Blog at php|arch

    • An error has occurred; the feed is probably down. Try again later.

  • Flickr Recent Photos

    Blue Parabola Southern Office-Rear Annex is closed for snowSnow Heart@dzuelke getting ready to give his talk@fabpot talking about Dependancy Injection@derickr giving the opening keynotePeople meeting other peoplePHP Benelux Goody Bag ContentsCheck InDSCN2280The main room

  • Categories

    • Apache
    • BlogBling
    • Blogging
    • codeworks
    • Entertainment
    • Entrepreneurship
    • Flex
    • Humor
    • JavaScript
    • Long Form
    • Management
    • Marketing
    • Me
    • PHP
    • podcasting
    • Programming
    • SQL
    • Technology
    • Web 2.0
    • wordpress
    • WordPress Plugins
    • writing
    • zend framework

  • Meta

    • Log in
    • Entries RSS
    • Comments RSS
    • WordPress.org


Postcards From My Life is proudly powered by WordPress
Entries (RSS) and Comments (RSS).