Jae Toole

5 Quick Tips for Writing Better PHP.

Adopting a Coding Standard

Adopting a coding standard is an essential practice to writing better PHP code that can immediatly make your code much better. A coding standard is a set of guidelines that specify how code should be written, formatted, and organized. By adhering to a coding standard, you can ensure that your code is consistent, readable, and maintainable.

PSR-2 was the go-to PSR for most PHP projects up until a few years ago. Now PSR-12 is the better standard to aim for as it’s the more modern sibling of PSR-2. PSR-12 provides guidelines for formatting code, such as using 4 spaces for indentation, using camelCase for variable names, placing opening braces on the same line as control structures and much more. You can read more about PSR-12 here.

To enforce a coding standard in your project, you can use tools such as PHP CodeSniffer or PHP CS Fixer. These tools can analyze your code and identify any violations of the coding standard, and can even automatically fix many issues for you. In addition, there are tools such as Laravel Pint (if you’re within the Laravel ecosystem) which can keep your code formatted nicely and help you get setup with a PHPCS configuration.

In summary, using a coding standard is a simple but effective way to improve the quality and maintainability of your PHP code. By adopting a standard, you can ensure that your code is consistent, readable, and easy to maintain by not just yourself, but your fellow developers.

Writing Efficient Code

Another important point to writing better PHP code is to write efficient code. Whilst this is a very broad term, hopefully my points below should help make this a bit clearer:

Minimize database queries

Database queries can be expensive in terms of performance. To minimize the number of queries your application makes, use features such as eager loading, caching, and pagination (which Eloquent and other ORMs make nice and easy for us). Also, consider using more efficient database operations such as using WHERE clauses or JOINs to filter results, rather than retrieving all data and filtering it within your application. You can also look at adding indexes to fields on slow queries to help give them a bit of a performance boost but I’ll write more about this in another blog post.

Use Caching

Caching can dramatically improve the performance of your PHP application by reducing the number of requests to external services and/or databases. Use a caching strategy appropriate for your use case, such as file caching, memcached, or Redis to help ensure the cached data can be accessed as quickly as possible. Most modern PHP frameworks help make life easier when it comes to caching such as Laravel which has a whole host of caching features straight out of the box.

Utilising Queues & Jobs

Many PHP frameworks have support for Queues out of the box, moreover, it’s not a very difficult process to bring these into even legacy PHP applications. Consider pushing longer, more intensive tasks out to the queue to be processed asynchronously in the background without timing out.

Writing Tests

Writing tests for your code is crucial for maintaining code quality and preventing regressions. PHPUnit is a popular testing framework for PHP, and there are many other tools and libraries available to help with testing. I cannot stress enough how writing tests can instantly make you a much better developer. You don’t have to be strict and adhere to TDD, but ensuring your code has at a minimum feature tests will help you out big time.

Debugging Effectively

Debugging is an essential skill for any developer, not just PHP developers. Here’s a few quickfire points to help you debug effectively.

Use Debugging Tools

PHP has several built-in debugging tools, such as error_reporting(), var_dump(), and print_r(). These can help you identify errors and debug your code more effectively. You can also use external tools such as Xdebug. In the Laravel world, you can also use tools such as Laravel Debugbar and Clockwork.

Use Logging To Your Advantage

Logging can be a powerful tool for debugging complex applications. Use a logging library, such as Monolog (included out of the box with Laravel), to log important events and errors in your code. This can help you identify issues and diagnose problems more easily as you can add in a whole host of information to the log context. You can also use external tools such as Sentry to help manage application exceptions giving you a clear context of any errors.

Break The Problem Down

When faced with a complex issue, it can be helpful to break it down into smaller, more manageable parts. Use step-by-step debugging and examine the state of your application. In addition, when I am personally faced with what seems to be an impossible problem, I sometimes go back to basics, forget everything I’ve built up in my mind about the issue so far, and start again looking at the issue from scratch. It’s amazing what we can miss!

Ask For Help

Debugging can be a frustrating and time-consuming process. If you’re stuck, don’t be afraid to ask for help from other developers whether that be in your workplace, or on various online communities. You could even ask Google or ChatGPT (had to throw that one in there)!

Use A Framework

Now I know even in 2023, there are a lot of cases where we can’t just simply bash in a framework and call it a day. We are still surrounded by legacy systems and managers scared that they’ll lose control of the system if they implement a framework (yes I’ve actually had that used as an argument). Whilst I’m not saying frameworks fit every use case, we’re now in an ideal position where frameworks themselves are derived from numerous different open-source components, packages and modules which we can use interchangeably. If you adopt what I call a full-feature framework such as Laravel, you’ll have many inherent benefits such as security, caching, storage, queues & jobs and more as well as a whole host of open-source packages just waiting to make your life that little bit easier.

In Conclusion

I hope that these tips may help some junior developers or developers wanting to get started in this fantastic industry. I appriciate that this is a very high-level overview for each of my points so should you want me to delve deeper, reach out to me on any of my social media (linked at the bottom) and I’ll be happy to expand on these points.

Do you need help with your project?

If you need help with your development project, I’d love to help! You can contact me here.