PHP optimization tips for any project

index

Optimization is part of every project. If you are a developer, it is essential for you to optimize your script early in the development process itself. Following the best practices while coding your PHP script is a good starting point to write a well optimized PHP code.

1.  Implement gzip compression for resources like fonts, js , css and images etc. You should use the following code in .htaccess file.

This can be used to compress the custom fonts.

You need to set max caching headers for every resources.

You can set max expire time for resources so theses files will not send request again to server until it expire it.

Gzip the resources

2. Apache & PHP & Mysql

Use PHP-FPM as mod_fastcgi.
Use PHP 7.0
Use Latest Mysql
Use PDO for mysql connection.

3.Check YUI Slow

Minimize HTTP Requests
Use a Content Delivery Network (CDN)
Avoid empty src or href
Add an Expires or a Cache-Control Header
Gzip Components
Put StyleSheets at the Top
Put Scripts at the Bottom
Avoid CSS Expressions
Make JavaScript and CSS External
Reduce DNS Lookups
Minify JavaScript and CSS
Avoid Redirects
Remove Duplicate Scripts
Configure ETags
Make AJAX Cacheable
Use GET for AJAX Requests
Reduce the Number of DOM Elements
No 404s
Reduce Cookie Size
Use Cookie-Free Domains for Components
Avoid Filters
Do Not Scale Images in HTML
Make favicon.ico Small and Cacheable

4. Lazy Loading: (you can use unveil lazy loading js extension http://luis-almeida.github.io/unveil/ )

5. You can use Cloudflare free plan for CDN images, JavaScript and css.

6. Disable Debugging Messages:

File operations are expensive. So, if you have written lot of custom functions to log errors and warning during your development process, make sure you remove them before you push the code to production.

7. Use Caching Techniques:

Use cache to reduce the load of database operations as well as the script compilation. We can use memcache for the reducing database load and APC for opcode caching and intermediate code optimization.

8. Use JSON:

Use JSON instead of XML while working with web services as there are native php function like json_encode( ) and json_decode( ) which are very fast. If you are bound to have XML form of data, then use regular expression to parse it instead of DOM manipulation.

Comments