PHP programming is a large jungle of events. Even people with years of experience working with PHP, can be seen doing odd and incorrect things out of lack of exposure to tips. Here are some of the most useful tips to go smooth with PHP coding and combat hurdles that come off often in your way.
1) Go OOP
If you haven’t started with OOP (Object Oriented Programming) yet, you are going to fall behind. This is because it is making programming faster with you not having to repeat codes and perform basic tasks repeatedly. With objects bringing functions together and grouping them in a productive manner, it gets really easy to get hold of procedures and practices. It aids to faster loading and logically-bound results applying less code – reducing your efforts to a minimum few.
2) Ditch Anything Ending With _once ()
As a PHP programmer most of us know that include () warns with a notification in case it fails, whereas require () destroys the script gravely when it fails. What most of us don’t know is include_once() and require_once(), or simply the ones ending with _once(), are highly heavy on server. Well, the sad part is we can’t help doing away with it as it is how PHP behaves. All you can do is plan your code properly and stay away from the evil.
3) Develop With Error Reporting On
You must have been hearing this from the days when you just started with PHP or when under training but you have not followed it that seriously like most of them. No matter how big is the task or how much time you will take to get the code done always keep the error reporting on. E_All should be your call. This way you will be able to track even the smallest of errors that might get heavier and deadlier later on. So, just keep it on all the way through before you actually step into the production mode.
4) Use a Framework If Needed
Okay, you are good at Core PHP and can do it quite efficiently. With that being said, you have to presume that you are flawlessly sharp with adapting to fast changing PHP practices and over that you are building something as basic and simple as ‘Hello World’ application, to get always right with your code. Well, this is true. Unless that’s your story, you will require a framework to make it get easier in most of the cases. So, get one to help you get going with least efforts with things already arranged and ready-to-use in a favorite framework.
5) Use PHP’s Inbuilt Functions
There are many little tasks that are best handled by the inbuilt functions in PHP. Take for example you want to count keys in an array. This could be done using the count () function which would save you from the effort of appending the value for each iteration in the loop. Such functions inbuilt with PHP are really helpful to get going with the regular tasks.
6) Use POST Not GET
Another important practice is to use POST and not GET, of course when possible. The reason is simple, it’s easy to emulate GET and therefore it increases the chance of your project getting hacked. Opt for POST when you can choose between the two. While doing things like form submission it will allow you all the safety you need.
7) Protect Your Database
You can best protect your database when entering a string value by using mysql_real_escape_string (). This will keep your database protected from malicious code attacks and keep it tracked for any possible threats. Further, while validating POST and GET strings, never use $_REQUEST and make sure data submitted with each form is appropriately done and has the right type and value, before it is added to the database query.
8) Understand Your Project
Last but not the least you have to understand your project and the underlying idea of it. Just like an artist can’t draw something without seeing it before, you cannot code something right without visualizing and conceiving it before. Make sure you only go ahead with the programming once you have all the idea about the project.