Top 10 PHP Tips for Developers

I always wanted to write an article like this because I think about it all the time – the 10 things that should be considered most important to convey to someone else? Well, after literally years of thinking I think I’ve come up with the best list I can think of. So, without further ado, let’s get to it.
Top 10 PHP Tips for Developers

Go OOP

If you have not yet entered the realm of object-oriented programming, then you are at a disadvantage, and are falling behind quickly.
Object-oriented programming is essentially a method of programming with the use of classes and objects, linking things together, eliminating the need to repeat code and perform basic production tasks very easily. Objects are essentially classes that collect a lot of functions together and wrap them in a container that can be reused again and again without the need to rewrite the functionality or procedures whenever you have to do something.
Procedural programming work following a routine from the top to the bottom of each page that the server reads all the files on your server side. With OOP, there may be one or two objects being instantiated, which in turn could instantiate a hundred or a thousand other objects that everyone could perform certain tasks based on variables passed in objects. Object-oriented programming is faster, simpler, easier to debug, uses less server resources, less code, is a faster and more logical work load once you figure out the basics. Go POO – changed my style of development forever.
Everyone knows that include (), simply give us a warning if an error occurs while require () kills the script with a fatal error when it fails. What we forget is that no include_once () and require_once () is extremely difficult for server resources. There is nothing we can do about it, is how PHP is configured. Just remember that these things kill the server resources, especially in a large frame, and if you plan your code correctly you will not even need anyway.

Stay away from anything ending with _once ()

Everyone knows that include (), simply give us a warning if an error occurs while require () kills the script with a fatal error when it fails. What we forget is that no include_once () and require_once () is extremely difficult for server resources. There is nothing we can do about it, is how PHP is configured. Just remember that these things kill the server resources, especially in a large frame, and if you plan your code correctly you will not even need anyway.

Develop With Error Reporting On

The first thing to do when starting a new project is to turn error reporting to E_ALL, and should only disable ten seconds before moving to production mode. I do this with every project I build and there is nothing better than running a project in full production mode and did not even get an error. Moreover, with reporting error, you pick up the little mistakes that will eventually grow to bite into the … well, you get my point.

Use A Framework If You Need One

Ok, so Rasmus Lerdorf says you should not use a framework that could fairly conclusively shows that a framework is much slower than normal PHP code when print a simple “Hello World”. Two things to mention here, however: Rasmus Lerdorf is not and I bet you will not be building a “Hello World” application every time you program something. Frameworks that help you do the tedious things can help, but you have to learn how to work the first frameworks to simplify things, but that’s the only real choice. Also you stand less chance of writing bad code when someone has written most of it for you, but let’s assume that I have not said that.

Use PHP’s Inbuilt Functions

Ok, you want to count the number of keys in an array? You can simply loop through the array and increment a value for each iteration, right? Or you can just use the built in PHP function count (), which does what it must. PHP has many built-in functions that can do what you need, so check the manual to make sure you are doing it in the best possible way.

Protect Your Database

The best and safest way is to use mysql_real_escape_string() for all database before it is added to the database. This function makes all strings safe in terms of quotes and other functions that can harm your database or contain malicious code, so use it to be sure you have taken the first step against protection of your data. Another thing you can do is validate all POST and GET strings, never use $_REQUEST, and make sure all form submitted data is of the right type and value before adding it to a database query.

Use POST Not GET

Ok, this is not always possible, but when is not really necessary, do not use GET, use POST. The reason is simple – GET is easy to imitate, all you have to do is add something to my address bar and can hack your project. Obviously GET is the easy way to do pagination and permalinks, but when sending forms especially, stay with the post, it is safer to use.

Draw Before You Code

A good practice is to enter wireframe your projects, even if you are just scribbling some notes on a piece of paper. It is really important to the mechanics of the application to think a bit before sitting down to start coding, since in the planning process you will really solve the difficulties in the head and avoid the headache that comes with the facepalm when you realize that everything you do is just wrong is not necessary, or just plain silly.

Understand Your Project

An artist can not draw something he has not seen before. A singer can not sing a song you have not heard before. You can not code a project that does not fully understand. If you do not understand exactly what to do and how you need it, you can not build.

Code Code Code

If I could get something through anyone reading this, that’s all. You can not be a good developer reading. You can not be a good developer to see someone develop. The one and only proven and reliable, is actually writing code. But – and here’s the catch – build real stuff! Do not go and code something that you have no interest in, or will never use. Build what you like, and you will be excited and interested in it, and you will learn. Then make it awesome, build upon it, and do better.

Leave a Reply

Your email address will not be published. Required fields are marked *