Skip navigation.

Probably the worst way to pad a string...

Wonky Code
Everyday I find code examples that make me go WTF. This is a prime example of one. It is a function that takes an item's id number and generates a left zero padded string with the id number. I guess nobody told them about the str_pad() function.
function getVanPic($item_id) {
    $pic_path = "/path/to/pic/dir";
    if ($item_id >= 1000000) {
        $strPicName = $item_id;
    else if ($item_id >= 100000) {
        $strPicName = "0".$item_id;
    else if ($item_id >= 10000) {
        $strPicName = "00".$item_id;
    else if ($item_id >= 1000) {
        $strPicName = "000".$item_id;
    else if ($item_id >= 100) {
        $strPicName = "0000".$item_id;
    else if ($item_id >= 10) {
        $strPicName = "00000".$item_id;
    else {
        $strPicName = "000000".$item_id;
    $strPicName1 = "w".$strPicName.".jpg";
    if (file_exists($pic_path."/".$strPicName1)) {
        return "van/".$strPicName1;
    $strPicName1 = "W".$strPicName.".JPG";
    if (file_exists($pic_path."/".$strPicName1)) {
        return "van/".$strPicName1;
    return "";

Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.

... or nobody told 'em about the *prinf() family

Honorable mention for "Use of Multiple Returns" in one function..

A blonde programmer working at our company once produced this code:

switch($num) {
case 0: $num = "00"; break;
case 1: $num = "01"; break;
case 2: $num = "02"; break;
case 3: $num = "03"; break;
etc.. all the way to nine

So it can always get worse you know! (guess noone told her concatenating strings)

Shouldn't that headline read "Probably the WORST way to pad a string..."?


Oh yea. My bad. Fixed.

CLEARLY, the best way to do this is through recursion :)

I wonder what the cumulative hits to memory would be for a function like:

function padnum($num,$spaces){

I wonder what the cumulative hits to memory would be for a function like:

function padnum($num,$spaces){
return $num;
return padnum('0'.$num,$spaces-1)

function pow10($spaces,$num=1){
return $num;
return pow10($spaces-1,$num*10);

As you can see, all problems can be solved by recursion. Eventually. Plus, it requires ingenuity to understand, a desirable trait in a maintenance coder. Thus it is the best solution to every problem!

(Caveat, pow10 may cause problems with negative input. It's a debugging feature one of our engineers needed!)

(*To site admin: You REALLY need to escape HTML, or cross-site scripting [ie, inserting arbitrary code and content] will bite you eventually.)

Hey, let's try it!
(The cross-site script, that is.)


Actually, I just found this web site today and I feel obligated to share something that has been bothering me for a long time.

It seems that being a good programmer is quickly becomming harder and harder to do. I don't mean going from 'good' to 'better.' I'm talking about getting from 'beginner' to 'just good.'

I think that the plethera of 5th- or 6th-generation scripting languages that seems to be growing daily has combined with the need to publish content quickly on the Internet to create an environment where nobody wants to admit that they need more beginner-level training. There are so many books, websites, etc. that tell us how to do a few really cool tricks, but there is very little out there that offers us a broad base of basic programming concepts, or that attempts to cover all or most of a programming language at a beginner level.

At my school (a 4-year college), for example, the "100-level" programming course is Object-Oriented Programming in C++. Whatever happened to C? Or what about something even simpler than that to give us a broad understanding of how programming should work?

Once you get the basics down, programming is all about syntax and tricks. Well, tricks as in knowing the _easy_ way to do something. I for one don't haven't coded a single function with SQL, LISP or many, many other languages, but after a few examples I'm quite sure I could read those.

Personally, I feel that 99% of the "teaching" sites should offer more examples, less theory. I don't need to know _why_ it works like that, just _how_ it works. And the best way to grasp that is see it in action.


online poker rooms - online poker rooms, texas holdem poker | online poker sites - wsop, poker supplies | wsop - party poker, poker books | poker online - poker tournaments, poker tables | internet poker - texas holdem poker, online poker | online poker sites - poker supplies, texas hold'em poker | texas holdem poker - poker rooms, free online poker | pacific poker - texas holdem poker, party poker | poker tips - WPT, poker games | empire poker - poker online, online poker rooms | wsop - poker tips, free online poker


world series of poker - poker stars, poker tables | poker games - texas hold'em poker, poker | poker games - empirepoker, texas holdem poker | poker books - WPT, texas hold'em | paradise poker - poker books, texas holdem | internet poker - poker, WPT | poker - online poker rooms, poker | online poker sites - online poker, empirepoker | free poker online - poker rules, poker rooms | internet poker - online poker rooms, texas holdem poker | pacific poker - paradise poker, poker books

internet poker

world series of poker - empire poker, poker rules | world poker tour - online poker sites, empire poker | poker chips - online poker rooms, internet poker | texas holdem - internet poker, online poker sites | online poker - poker games, WPT | poker books - texas holdem poker, free online poker | online poker rooms - poker tables, texas hold'em poker | wsop - poker online, poker rules | texas hold'em - paradise poker, poker online | online poker rooms - partypoker, online poker | poker books - online poker rooms, poker tables | poker stars - poker rooms, paradise poker | texas hold'em - poker tables, online poker rooms | poker books - free poker online, empirepoker | poker tournaments - poker games, poker | online poker - poker tournaments, poker online | poker tournaments - poker rooms, WPT | texas holdem - texas hold'em, party poker

Post new comment


  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <pre> <p> <br>
  • Web and e-mail addresses are automatically converted into links.