These are 5 tips that I use and recommend. I know there are plenty of lists like this, but I hope you learn at least one new trick...
- Be Consistent
PHP is a very flexible language, which means there are usually multiple ways to do anything. This gives you a lot of freedom, which means freedom to make a mess! This is why it is very important to be consistent with your code. If there are multiple ways to accomplish something, pick one and use it every time! This also applies to placement and spacing of braces and naming conventions.
- Use Meaningful Names
When naming a variable or function, take a few seconds to pick the best, most descriptive name. Give it a name that describes exactly what the variable or function is used for. Avoid generic names like $temp or do_stuff(), and avoid abbreviations unless they are well-known. However, don't go overboard with excessively long names. Keep everything as short as possible while being as descriptive as possible, so take a few seconds to think about the name.
- Use Whitespace
Whitespace is any space that does not contain code, such as tabs and line breaks. PHP ignores whitespace among code, so you are free to use it for formatting without affecting functionality. Effective use of whitespace makes your code so much easier to read.- Indent blocks of code, using a tab or multiple spaces
- Separate sections of code by functionality using line breaks
- Remember to be consistent
- Use Constants for Configuration
Most projects have a configuration file or section to set things such as database details or paths to files. These usually never need to change during the execution of the script, so instead of using normal variables you should define constants. This will make it easier to separate configuration from other variables by naming them with ALL_CAPS and using a unique prefix.
- Modularize With Classes
This last tip might be the most radical change for some, but it greatly improves the organization of your projects. It is to use classes to store code with similar purpose, especially if it will be used in multiple places. Writing classes might seem strange if you are used to the old procedural style of coding, but it is simple once you learn the basic differences.
The benefits of classes and objects is not only re-usability, but that they are completely self-contained and isolated. This means that variables declared inside classes won't affect variables in other places and can't be affected by other variables.
Placing raw code in classes instead of the main flow of your scripts also improves readability. You should be able to quickly determine what your code does based on the names of the class and methods used, instead of interpreting raw code.
One more tip to add here is to store each class in a separate file. This way you can edit specific functionality by editing that file without being distracted by unrelated code. To make this easier, you can autoload classes as they are called in your scripts (see: Autoloading Objects).