Top 10 Tips for PHP Programmers
I thought that it was time for me to give a few helpful tips to those PHP wizards out there on how to write better scripts. I wanted to provide some tips that are a bit different than what you may have read on other blogs or seen in other articles on the subject. While some of these tips are universal (and thus you may have seen already), I am hoping that a few of these give you a different perspective on the ideas of what makes good PHP programming. In addition, I try to mix in a bit of my own philosophy into these. As someone who has come into a lot of projects and cleaned them up, over the last 16+ years, I think I have had a pretty good amount of experience in what makes good PHP work. Don’t use these as hard fast rules, but just guidelines to make your code a tad bit cleaner. By doing this you can help me, and other developers, out in the event we end up looking at your code one day.
Top 10 PHP Programming Tips
- Use classes and functions to the best of your ability to manage complexity – We all know that you can write a script in PHP that doesn’t have a single custom function or class anywhere to be found. Sometimes that is fine especially if the script is on the order of 50 – 200 lines (even though line count shouldn’t be a determining factor of whether or not one should create functions or classes). I have written many utility scripts that are straight PHP procedural code. However, once the script starts growing in length and complexity, leveraging functions and classes can keep things simple. This principle is not just for PHP, but programming in general. PHP is definitely a language where complexity can grow rampant quickly! So always think about when it is a good time to use functions. You will quickly discover that once you create a nice reusable function in PHP, it will serve you well in a ton of other scripts. That idea alone should encourage you to go to functions as soon as you can!
- Watch your return values like a hawk – One of the most common complaints about PHP is that it lacks consistency in the language itself. Some functions return null on error, some return false, some return -1 or some may throw an exception. You may start adopting this pattern of inconsistency in your functions and you should try to stick to a consistent method across all your functions. Don’t write a function that in one instance returns false on error and another where it returns an empty string; especially if both functions are designed to return a string. Look at what the function is designed to return and pick the best return value that fits. It should also fit with what other functions are expecting it to return.
- Try to steer clear of nulls where you can – As a tie-in to tip number 2, try to avoid returning null everywhere. By reducing the amount of nulls being passed around you reduce errors related to calling methods on null objects or having to design complex null tests. The only time I really think about returning a null from a function is when the function is expected to return an object. In this case I can plan that there may be a null coming back from that function. Perhaps it doesn’t make sense to return an empty string from a function that you are expecting returns an object. All I can really advise is try to keep the number of nulls down and try to opt for empty strings, false, or error codes / exceptions rather than returning null. But I found that nulls in PHP are just a part of life. Just make sure you also don’t create some crazy mechanism JUST to avoid using a null. Nulls are ok when used sparingly… like butter in your diet. 😉
- Keep it as simple as you can – This tip goes for all programming languages. Yeah we like to be clever, but simple is always better. Nice simple code with great variable names, function and class names and simple tests go a long way in readability and reducing complexity. If you can read a function like you can read a book, all the better. Believe me, your future self will be grateful if you make things as simple as can be. Once everything is rock solid, then perhaps you can go back and optimize “a little”. Even then simple straight forward code is going to help you do that.
- Leverage comments – Ok, lets put a stake in this right now, comments are a good thing when used in moderation. You SHOULD have comments. There is a movement out there that is saying that your code should be self documenting. That is true, it should be, but doesn’t mean that you can leave out comments. Your code should show the “how” and the comments should show the “why”. You can document through code that a variable should be the result of calculating interest against a loan over N terms. Your comments should show why you need to do this. Is it to fix a bug? Is it in response to a business requirement? Is it to compensate for a bad system that you can’t fix? As long as you keep the comments straight and to the point, and that you are not commenting every line, comments are good. I strive to keep a single line of commenting equal to 5 – 10 lines of code. But there is no hard fast rule about this. If you find yourself commenting a lot then it usually means you are violating tip number 4 and your code is not being as simple as it can be.
- Put the PHP manual website on your bookmark bar – Due to the problem I mentioned in tip 2, where PHP itself lacks a bit of consistency in the language itself, you almost always have to reference the online manual to see which order parameters go into functions, what functions return or what versions of PHP a particular function can be used in. The manual itself is very good in breaking down parameters, return values and showing some simple examples of their use. Be sure to always look at any warning boxes on the page! More often than not you will find a function that looks like it will work for your situation only to find out that it won’t work due to some problem outlined in a warning or error box. One last thing I will mention is the user comments. They are great but PHP.net has a habit of keeping comments up for years and often times examples may refer to PHP from 8 years ago and just doesn’t apply anymore. Always take comments with a grain of salt and verify if they still work.
- Use PHP 7 whenever you can – PHP 7 has been a fantastic change of pace for PHP. It has an increased speed which is noticeable (and keeps getting faster), it has removed a lot of old legacy code that has bad practice in it, it introduces newer concepts which have served other languages for years and can even cut down your development time greatly. I know it is hard to find hosts out there that have moved over to PHP 7 still, but there are more and more popping up all the time. PHP 7 will just make PHP that much more enjoyable to work with.
- Readability is paramount, so use good variable names and simplify test conditions! – To go with tips 4 and 5 I recommend always making use of good variable names and simple test conditions to keep your code readable and maintainable. Good names and simple tests help you simplify and reduce complexity by an order of magnitude. Plus they become self documenting and increase readability. Again you should have code that almost reads like a book and paired with good comments, you have code that is solid and easy to understand for when it needs a change. This rule applies not just to PHP but to other languages as well. Readability is one of the fundamental pillars of programming!
- Read others code – It is said that you can only get better when you are surrounded by others who are better than you. You need people who bring new ideas, new ways of doing things, new perspectives on old problems. If you read their code and see what they did, you can learn from them. In turn others will then learn from you. It is a never ending chain of learning that will help everyone. The more code you read the more you learn and the better you become. It is just that simple. I am nearly 20 years in as a developer and I still read other’s code often.
- Use frameworks only when they make sense – Lastly a small tip on frameworks like Laravel or CakePHP. Use frameworks only when they make sense. There is no need to add a whole system if your script is only going to be a few hundred lines max. Frameworks are awesome and have their place like everything else, but they often add complexity to simple projects. They can bloat your tasks and really be a pain to debug for simple projects. However, if your project is going to grow into something much bigger, frameworks can simplify the process and work to your advantage. It is up to you to evaluate frameworks, look at what they solve and if your project is the right size and fit for them. Always have a good reason to use a framework and that reason shouldn’t be “because it is what everyone else is using”.
In this article we covered 10 great tips for PHP programmers and I hope that you found them useful and enlightening. If you start with these and grow from there you will find that your PHP programming with become that of legend. These tips will also save you time and in some cases money if you use them for client work. But by no means is this all of the tips out there and you should definitely find what others have to say about the topic. Take what works for you. However, I encourage you adopt many of the tips above and use them in your work. People like me, and other maintenance programmers that follow you (maybe your future self), will certainly appreciate it. Also, if you have some great tips that have helped you out over time, be sure to share them in the comments below. I am sure others in the community would love to read them.
I wanted to thank you for reading! If you are interested in applying these tips on your next web project and looking for that project, consider “The Programmers Idea Book” offered here on the Coders Lexicon. It consists of 200 programming project ideas, many which can be done using PHP and have a ton of different levels of difficulty. If you are interested in learning more development tips, also check out our ebook on diagnosing the problem. In that book we show how to break down problems and debug them with useful tips and tricks to become a rockstar developer. 🙂
About The Author
Martyr2 is the founder of the Coders Lexicon and author of the new ebooks "The Programmers Idea Book
" and "Diagnosing the Problem
" . He has been a programmer for over 25 years. He works for a hot application development company in Vancouver Canada which service some of the biggest tech companies in the world. He has won numerous awards for his mentoring in software development and contributes regularly to several communities around the web. He is an expert in numerous languages including .NET, PHP, C/C++, Java and more.