Everyone knows that PHP has such a bad rap for so much bad code being out there. I am a firm believer it is not a problem with PHP, itself, but rather the incredibly low barrier to entry (open source, readily available with nearly all web hosting packages, etc.) coupled with Wordpress (written in PHP) powering a large majority of the web, making it so easy for everyone and their mother to publish poorly written Wordpress plugins and spew bad code out there.

For those of you who are proud PHP programmers, what do you to optimize your code's performance?

For me, I cache. I use Memcached. I use Redis. I use Cloudflare's caching mechanism.

I use a lightweight MVC framework, and I rolled my own simple ORM.

How about you? ...

Recommended Answers

All 6 Replies

  1. While I only have worked PHP in passing, you already hit the #1 accelerator known (caching.)
  2. The most common time consuming area I found which you usually see used by new coders is "Select *". There are long discussions about this but the source is easy to find why folk still do this. It's in almost every SQL 101 class and textbook.
  3. You may have already done this but to keep all the ideas in this list, consider a PHP Accelerator. Read https://en.wikipedia.org/wiki/List_of_PHP_accelerators
  4. Remove extra code. Sometimes we put in code to debug PHP but forget to remove it later.
  5. See if foreach is faster than a for loop.
  6. And if you can use a profiler, do that to find out where the code is spending the most time.

Much of the above applies to other languages so here's one of my favorite speedups I used over the decades. When do a repeating calculation, consider a lookup table for the answers. Be aware this is from my work in embedded computing where we don't have a FPU most of the time so looking up a SIN(x) value rather than computing it can be many times faster.

Use Opcode Caching: Enable Opcode caching like APC (Alternative PHP Cache) or OpCache to reduce script compilation time and increase performance.

Use SQL Prepared Statements: Use SQL prepared statements instead of building SQL queries on the fly to prevent SQL Injection attacks and improve database performance.

Optimize Database Queries: Use appropriate indexing, reduce the number of SQL queries, and optimize queries to reduce database load.

Use a PHP accelerator: Use PHP accelerators like APC or Zend Optimizer+ to speed up PHP code execution.

Minimize file system access: Minimize file system access by reducing the number of file system calls, using caching, and optimizing disk I/O.

Use efficient algorithms: Use efficient algorithms to minimize execution time and reduce server load.

Use a Content Delivery Network (CDN): Use a CDN to deliver static content like images, CSS, and JavaScript files to improve performance.

Use a PHP profiler: Use a PHP profiler to identify bottlenecks and optimize code performance.

Use the latest version of PHP: Upgrade to the latest version of PHP to take advantage of new features and performance improvements.

Use a caching system: Use a caching system like Memcached or Redis to cache data and reduce server load.

commented: Good answers +34

Hi Dani, below is what i recommend.

When it comes to optimizing code performance in PHP, many best practices can help. Here are some of the most important ones:

Use caching: Caching can significantly improve the performance of your PHP code. For example, you can use the APCu PHP extension to cache objects, functions, and data.

Optimize database queries: Inefficient database queries can be a significant bottleneck for PHP applications. Optimize your queries by avoiding SELECT * statements, using indexes, and minimizing subqueries.

Avoid excessive file I/O: Reading and writing to files can be time-consuming, so avoid doing it too frequently. Use techniques like file caching and buffering to minimize the number of file I/O operations.

Minimize external API calls: External API calls can be slow, so avoid making them too frequently. Use caching and batching to minimize the number of API calls.

Optimize algorithms: Use efficient algorithms to solve problems in your PHP code. Avoid using loops that iterate over large arrays or databases, and instead, use tools like binary search and hash tables to find the data you need quickly.

Overall, there are many ways to optimize PHP code performance, and taking a holistic approach is essential. By focusing on caching, optimizing database queries, minimizing file I/O, reducing external API calls, and using efficient algorithms, you can significantly improve the speed and responsiveness of your PHP applications.

what do you to optimize your code's performance

Profile first. Always. There's no point in optimizing your SELECT if it's called only once, but your code is full of nested foreach's.

Use caching: As you mentioned, caching can significantly improve performance. Caching stores frequently accessed data in memory or on disk so that it can be accessed quickly without having to recompute it every time.

Optimize database queries: Poorly written or inefficient database queries can be a major bottleneck for PHP applications. Use indexes, limit the amount of data returned, and avoid using "SELECT *".

Minimize file system operations: File system operations like reading or writing to files can be slow, especially when dealing with large files. Minimize the number of file system operations and use caching to reduce the need for them.

Use a lightweight framework: Heavyweight frameworks can add a lot of overhead and slow down your application. Consider using a lightweight framework or rolling your own framework to keep things simple and fast.

Optimize your code: Optimize your code by avoiding unnecessary function calls, minimizing looping, and optimizing conditionals. Use profiling tools to identify bottlenecks in your code.

Overall, it's important to follow best practices, use caching, optimize database queries, and use a lightweight framework to optimize PHP code's performance.

The main points have already been mentioned (like OPcache , FPM etc ) so I will add a few minor ones.

Request scope caching for prepared statements: There is no need to create a new PDOStatement object (and the db request that goes along) for every same sql statement that you use. If you use this statement only in one part of your code then probably you could use the same PDOStatement object but if there is a change to use it somewhere else too would be a good idea to cache it at least for the request life span.
example here : https://www.daniweb.com/programming/web-development/tutorials/500118/a-simple-data-layer-for-oop-php-applications

Application scope caching: There are some objects that their scope is application wide and change rarely compared to how often are accessed. An example would be a List of eshop categories objects. I have tried Memcache for this but many years now I use an implementation that have been inspired by @rubberman answer to: https://www.daniweb.com/hardware-and-software/linux-and-unix/threads/466034/load-files-of-a-folder-to-memory about Linux RAM disks .

Over the years I have tried other ideas as well , as making a PHP app run continuously and accepting requests through a port - eliminating the need for serialization / deserialization for app scope objects + implementing a DB pool mechanism with some help from PHP-CPP . But those attempts never made it to a production app mainly because of stability issues (even to minor PHP version changes) . PHP hasn't been created for that usage , and it was simpler to switch back to Java for those projects than forcing PHP to behave in a way the doesn't suppose to.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.