Problem Solving – guide to PHP programming and debugging.

Problem solving forms part of thinking.

Interesting... so, if so many web devs and IT personnel have such a problem with problem solving, perhaps they're just not thinking? Let's explore some of the problems I've seen people encounter, and how I was baffled they lacked the problem solving skills to complete their tasks.

All too often, a situation will appear in just one or two flavors. One being, "Hey, this should be working but it's not..." or, "Hey, I'm stuck." Sometimes you get the nice and juicy combo. But, that being said, let's explore how I most commonly find myself approaching these two generic scenarios.

Number one, "Hey, this should be working but it's not..."

Whenever you get to this point, that usually means a couple things:

  1. I have a pretty good idea on what should be happening
  2. I know how it should be done
  3. I've already solved this problem before, but this time it doesn't seem to be working

Number 3 is always the worst, because as a programmer, you always think back to the last time you faced a similar problem, and basically copy the way you fixed it before. If that is the case, examine what's different this time... versions, updates, different variables, etc etc, these all have an impact.

And what it really comes down to, is back tracking all of your steps.

  • Did I upload the latest version of the PHP script?
    • And if so, are the changes reflected on the server side?
  • Has someone else made changes to the files in use?
  • Have a double checked the process against my flow charts?
  • Have I tried basic debugging?
    • This means, rather briefly:
      • Echo out the variables BEFORE they're passed or referenced
      • AND AFTER they are passed or referenced.
      • Have I done a var_dump()
      • Have I turned on error_reporting(E_ALL);
      • Have I checked my error_log
      • Have I checked my mysql_error();

If you go through a simple list like that in your mind, you can quickly determine where you're missing a step.

And please, for the love of all that is holy, please, don't be afraid to TRY something even if you're not sure it'll work. You should take the basic steps to plot out your thoughts before you begin to code. For me, that usually means getting out some paper and a pen, and sketch out my idea with some notes.

Then I begin to code.

If I ever get lost in this new idea of mine, I refer to my original plan. If I realize then that my original plan was FUBAR, I simply make the adjustments to my sketch, and keep on going.

I feel that most web developers somehow feel that they can just jump straight into a project without any planning. It simply doesn't happen. All "great" projects, they start with a little planning. Sure, maybe they didn't have a road map, version control, diagrams, database designs, ticketing systems, et cetera, but they at least had an idea. And I guarantee that idea was at least written down in pseudo code.

Again, the idea here is that you HAVE a plan, and you know how things should work, so stick to your plan. You run into the problem of "It should work but it's not" when you've altered a step somewhere. Check your debugs, check your error logs, check your plan. Rinse repeat until you go mad. And don't be afraid to ask for some fresh eyes on the problem.

Number Two, "I'm stuck."

This comes down to just general programming knowledge. Or rather, how well you understand programming logic. I will often tell people, any one can be a programmer; in fact, everyone is a programmer. Programming is just solving a problem with at last one solution. You do this by breaking it down.

Identify the problem - What do I need to accomplish?
Determine the order - What needs to happen to solve this problem?
How do I apply my solution - What's my plan of action?

Let's take for example, the problem of creating an E-Mail list manager.

Identifying the problem - I know what the solution is -- an email list manager. That's my solution, now what's the problem?  Ok, this e-mail list manager is going to have a set of emails that it sends updates to everyone month. So... the problem is I need to find out how and where the e-mail addresses are stored, and I need to create a way for them to automatically be sent.

Determine the order - Well, I kind of already figured that out. I need to:

  1. Get the E-Mail Address
    1. Make sure it's valid
    2. Make sure they are opted in
  2. Add that Validated E-Mail address to the 'Send To' array
  3. Create the body of my E-Mail
    1. One for Plain Text
    2. One for HTML
  4. Apply any templating system that may be needed
  5. Do a foreach() on the 'Send To' array
    1. Then send the emails

How do I apply my solution - Well, I'm going to need a CRON script that will run once a month. I will need to create a debugging / status message system so that I can record how many e-mails got sent out, and on what date, and to whom. It's pretty straight forward from there. Create the PHP script, be sure to apply the CLI items, and boom. Start.

What I'm trying to get across here, is that you need to approach things not as strictly code, but as a single thought. This is what I want, this is how I do it.

When you're running a program, you're not just running code, you're running someone's thoughts.  That's the beauty in programming, it's not just about the code, it's about someone else getting to see into your mind.

Sunday, March 2nd, 2008 Computers, PHP, Software Development, Tips

2 Comments to Problem Solving – guide to PHP programming and debugging.

  1. I enjoyed this article on problem solving in programming. I’m fairly new to php programming. I’ve taken a couple of classes, but they’re mostly monkey-see, monkey-do stuff. For example, in the last class I took, we built a shopping cart, yet I lack the skills to attempt the email list you described above. I don’t feel like I really got much of a feel for how to wield the php language–I feel like I dare not take my training wheels off. I can barely manage to alter a script to suit a particular purpose and I usually feel lucky when I succeed, rather than confident about how I achieved the desired outcome. I know the only way to become proficient is to practice, so I’m looking for a book or course that will give me practice in these things. Sort of an exercise book of progressively difficult problems to solve– similar to a math book where you’re given a skill, then a set of problems to solve using that skill. Then you’re given another skill and a problem to solve that requires that skill and previously learned ones.

    Do you know where I might find such a course or book at an affordable price (under $100)?

    Again, I appreciate your insight about programming. I will visit your site again.

    Thanks,
    Lanore Dixon

  2. Lanore Dixon on December 22nd, 2008
  3. Lanore,

    Confidence comes with time, and ultimately, it’s one of the most important attributes a developer can have. I’d recommend not to doubt yourself, just be open to change and advice.

    As far as books go, there’s a few PHP books that I would recommend.

    1. PHP Hacks: Tips & Tools For Creating Dynamic Websites
    Amazon Link

    2. Essential PHP Security
    Amazon Link

    3. PHP Cookbook (Cookbooks (O’Reilly))
    Amazon Link

    The first book, PHP Hacks (ISBN-13: 978-0596101398) is a great book that covers practical PHP scripts and snippets you’ll want to use on sites. It also covers more advanced topics such as design patterns for PHP. This is a book I recommend for both beginners and advanced users.

    The draw back is, it’s more learn from example than from courses.

    What you should take from this book is how it combines technologies to form a solution, and how smaller solutions can combine into a product.

    The second book, Essential PHP Security (ISBN-13: 978-0596006563) is a great book that covers security in your PHP scripts.
    From log in systems, to how people break into your sites using SQL exploits.
    This is more of a crash course in security for your site.
    Great book, everyone should get it.

    The third book is a cookbook (ISBN-13: 978-0596101015), that will break down common usage items. Since most of the time when you think of a general solution, it takes several smaller solutions to form that, you can reference this cookbook on how to do those.

    I think the point I’m trying to make here, is that if you can figure out how to do smaller steps, you can then combine those steps into larger solutions.

    For example, you learned how to make a shopping cart, well, that means you now know how to take user input, display output, and generate content based upon that user input. Those steps could be used to do something else, such as tag clouding, or news system, or blogging.

    Don’t focus on the solution, focus on what the problem was, and how it was solved.

    Hope that helps!

  4. dschreck on December 22nd, 2008

Leave a comment