General Tips

PHP Tips

Cool just a few little tips i’m picking up along the way while learning PHP that i think are useful to know. This list will grow as I get better so keep checking back.

Firstly I would like to mention commenting.

Commenting is bloody useful because if you write code and then leave it for 6 months then want to go back and edit the site, you’ll either have to have a great memory or have commented your code. Now to do this you simply do the following in you PHP scripts. It serves as a way to jog your memory when editing code or to instruct others who would be reading the code.

<?

// this is a comment, anything on this line will not be processed.

?>

Or if you have more to write, you can do this:

<?

/*

This is a comment

So is this…. And this

is too

*/

?>

Error Checking

Now you’ll all get them, nobody writes perfect code and i can assure you there will be times you will spend an age checking your code for errors to find you have missed 1 character or 1 word, and needless to say you’ll kick yourself, hard!

So to avoid this, say for example : if you get any kind of Unexpected error, (unexpected string or unexpected variable) the FIRST thing you should do is go back and check you have put a semi colon at the end of the previous line or closed off you if statements/loops/functions with curly braces!!! or if the line has a string and a variable in it, make sure you haven’t missed any dots/quotes!

Also copy and paste the error message into google, PHP is globally known language and someone is bound to have had the same problems as you before so check out the web for fixes.

It is also a good idea to set error_reporting in php.ini to E_ALL while developing to make sure you get all errors before uploading your code.

Plan

Think about the scripts you are going to write, if necessary plan them out on paper before hand. It will save you a great deal of time when it comes to development. Write down what you want your site to look like, how you want information to flow and where to flow to. Planning is essential no matter how big or small the project. the best developers well the ones i work with are always scribbling on scrap paper. Designing flow charts and site layouts etc etc. So plan plan plan!

Databases

If your website needs to connect to a database for any reason it is generally a good idea to store that database file in a different directory to your webpages, assuming you’re using MS Access. A|lso another good idea is to write the code that you use to connect to the database and place it in a separate file, then include that file in the rest of your pages, thus allowing you to reuse that code for database connectivity.

Object Orientation/Code Re-Use

I’ve had this drilled into me by a friend of mine who is a developer, write code so you don’t end up writing the same thing over and over again. If you need a function to draw page elements such as header and menus. encapsulate that code into a function and make sure you call it on each page you need it. That way you save yourself time and effort. You may have to put a bit more thought into your code but the pay off will be great. Once solution is to use objects.