Latest Posts »
Latest Comments »
Popular Posts »

5 Things I wish I had known about PHP

Written by Bean on April 13, 2009 – 3:19 am

Like most independent geeks, I started off by teaching myself PHP. I already knew Java, PERL, BASIC and ASP so many things about PHP were easy for me to learn. I rolled up my sleeves and went to work  coding interactive PHP web sites. I drank gallons of caffeine and cruised the PHP forums  at all hours of the night, seeking answers to my PHP questions. I could have saved a lot of time, energy and sleep if I had been taught these five things about PHP.

1) Code first, style second

The code is the backbone and structure to your web site.  Code your PHP, HTML and JavaScript before you dress up your pages. Make you code clean, make it solid and make it ugly. It is much easier to debug and validate your code without the styling. Once you have the basic structure in place and functioning, start adding your styles. If your styling is dynamic, for example a style sheet determined by a user selection, add in your logic

2) Follow separations of concerns standards

Separations of concerns means segmenting your code so the HTML, the PHP and the styling are kept separate. At the top of your document, place as much of your documentation and scripts as you can. Make it as easy as possible to find your variables and code. Use linked CSS style sheets instead of embedded code.  Finding that missing semicolon is hard enough in reading through 30 lines of code. Sorting through 80 lines of code with tags scattered throughout is a nightmare that I lived through way too many times.

Go a step further, once you have got your custom PHP functions operating smoothly, place those functions in include files with excellent documentation.  This makes your code more modular and easy to reuse.

3) Well documented code saves time

OK, I had this drummed into my head in college, but that was for school, not for real life, right? Boy was I wrong! The first time I had to upgrade my code after two month intermission, I was saying all sorts of naughty words. The blue language and my foul mood could have been easily eliminated if I had documented my all of my PHP variable declarations, my loops and my functions.

4) Creating a mail form is easy, creating a secure mail form not so easy

The PHP mail function looks easy but looks are deceiving. Mail is a spammers’ gold mine and they spend a lot of time and energy trying to get your mail functions to work for them. It takes diligence to stay on top of their tricks. I have learned that using a current 3rd party script, from a reliable source, is the way to go. I don’t have the time or the intelligence to keep up with the spammers so I rely on those that do and keep my mail forms secure.

5) DOCUMENT _ROOT is your friend

I am a big fan of includes. When Vikki taught me about includes and SHTML here at LVS Online long ago, I was ecstatic! It made much more sense to me than templates but I struggled with paths. How could I make my footer include work in my home directory and my nested directories. My navigation includes kept me up way too late at night. I am embarassed that my first PHP sites would have include folders for each level of directory and yes I would edit two levels of includes and forget to edit the third ARRRRGH.

The PHP superglobal server variables were something I didn’t discover until over 6 months into my PHP ventures. $_SERVER["DOCUMENT_ROOT"] solves all of your include and multiple directory issues.


Tags: , , ,
Posted in PHP | 1 Comment »

One Response to “5 Things I wish I had known about PHP”

  1. Saravanan R says:

    Hi,

    I have been working in PHP Platform since 1.7 year.I think you have given us a very valuable information about PHP. Keep posting this type of comments.

    Regards,
    R.Saravanan

Leave a Comment

Programming Blogs - BlogCatalog Blog Directory