PHP: Learn by Doing, Learn Faster by Breaking

Some quick tips to the budding PHP programmer. You will always break things, but that’s how we learn. This will hopefully help you to avoid some of the problems I met as a young programmer.

PHP is a vast language. There are about 10-20 different ways of

accomplishing the same exact thing. A great programmer knows all of

these methods, but a great programmer I am not. But my experience has

done me well, and I will pass on my thoughts to you.

For the most part, I am self taught when it comes to PHP.

My roots

start with Perl, a language that was taught to me by my friend Ben. I learned a lot from him, and

I developed a strong base for when I dove into the world of PHP. At the

time, however, I had no contacts for other PHP programmers. And such, I

set out about hacking my way through the language.

I found that it was best to set goals. I found something that I wanted

to accomplish, and then I researched how I would do it. Once complete,

I would then set about finding a better way to do it. I ended up with

several projects that never amounted to much. But as I grew, each goal

or project grew in complexity. Eventually, I learned enough to embark

on a massive project. And the Swing-CMS engine

was born.

Albeit, what you see here today wasn’t my first attempt. Originally, I

wrote a very simple CMS that I called “Pivot” for my group,

href=”http://fulcrum.antisoc.net”>Fulcrum. Being naive, I developed

my own template engine that would search and replace tags with PHP

scripts. Ultimately, the system worked, but it was flawed. Armed with

knowledge from “Pivot”, I moved on to the first iteration of Swing. It

was a step in the right direction, but it too was flawed and the big

problem was once again the template system.

Finally, I developed a pretty solid core for my engine. And I got here

because I really broke a lot of things. But I didn’t have the benefit

of having someone like me offering advice. Sure, you’ll still break

stuff, but hopefully you won’t run into the same problems that I had.

Here are some quick tips that I can offer to you while you’re learning

PHP

  • Plan Ahead

    You can’t anticipate every problem

    that may arise, but plan ahead. Frequently used aspects of your

    application should be stored in functions or classes so that you can

    call them from anywhere.

  • Format Your Code

     Follow standard coding

    guidelines. Take advantage of whitespace for organization, indent

    content within loops, functions or classes and develop commenting

    styles.

  • Use PEAR

    For those of you who aren’t aware, PEAR is a depository of high quality (and widely used) PHP extensions. Most

    of your common applications are here. Want to easily connect to the

    database and manage your queries? Several solutions to choose from

    (PEAR DB is the most common). Conversions between XML and BBCode, it’s

    there. This is a great place to start to save yourself time.)

  • Use PhpDocumentor

    Especially for your larger

    applications, it can be hard to keep track of your functions and

    variables. PhpDocumentor requires you to comment in a special way, but

    it’s worth it. From your comments, it will generate HTML documents that

    will become a wonderful resource. PhpDocumentor is available at the

    PEAR website.

  • Use Smarty

    Smarty, as I mentioned earlier, is a template system for PHP. It is very useful in writing web applications

    as it helps you keep your code and your HTML templates separate. It’s

    easy to use and there’s no reason to write your own template system.

  • Never Work on Production Source

    Even if you don’t

    have a production installation, it’s a good idea to work on a copy of

    your code. You never know when something you do can royally mess up a

    project, and it may be hard to revert back to where you were. At the

    very least, work off of a copy. If at all possible, try using a CVS or

    a Subversion repository to keep track of your code. If you’re working

    on an Open Source project, check out SourceForge

  • Experiment

    Don’t be afraid to take risks. If

    you’re following my advice above, you’re working on a copy anyhow, and

    you can easily revert to an earlier version if you break something

    serious.

  • Share Your Code

    There is no better way to learn

    than to share your code with others. Asking “How do I do this?” isn’t

    going to get you nearly as much response as if you were to post your

    broken code and ask “Where did I mess up?” But this advice goes beyond

    learning – this is about respect. Posting your code shows two things:

    1) You want to learn a better way to do things and 2) You’re willing to

    contribute. PHP gurus like to help those who will carry on their

    legacy.

  • Get Involved

    Sometimes, the best project is one

    that’s run by someone else. There are plenty of projects looking for

    help on SourceForge (like mine). Just be honest with the project leader

    about your skills. They often won’t turn down help. Even if you’re

    mostly beta testing, there’s lots to learn from a large project.

    Besides, it’s a good resume builder if you’re in the field.

Leave Your Response