PHP optimization tips for any project
- Digital Engineering
- Ecommerce
PHP optimization tips for any project
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.
1 2 3 4 |
# Add SVG Mime Types AddType image/svg+xml .svg AddType image/svg+xml svgz AddType x-font/otf .otf |
You need to set max caching headers for every resources.
1 2 3 |
Header set Cache-Control "max-age=29030400" Header set Cache-Control "max-age=604800" Header set Cache-Control "max-age=2700" |
You can set max expire time for resources so theses files will not send request again to server until it expire it.
1 2 3 4 5 6 7 8 9 10 11 12 |
ExpiresActive On ExpiresByType image/jpg "access 1 year" ExpiresByType image/jpeg "access 1 year" ExpiresByType image/svg "access 1 year" ExpiresByType image/gif "access 1 year" ExpiresByType image/png "access 1 year" ExpiresByType text/css "access 1 month" ExpiresByType application/pdf "access 1 month" ExpiresByType application/x-javascript "access 1 month" ExpiresByType application/x-shockwave-flash "access 1 month" ExpiresByType image/x-icon "access 1 year" ExpiresDefault "access 2 days" |
Gzip the resources
1 2 3 4 5 6 7 8 9 10 11 |
AddOutputFilterByType DEFLATE text/plain AddOutputFilterByType DEFLATE text/html AddOutputFilterByType DEFLATE text/xml AddOutputFilterByType DEFLATE text/css AddOutputFilterByType DEFLATE application/xml AddOutputFilterByType DEFLATE application/xhtml+xml AddOutputFilterByType DEFLATE application/rss+xml AddOutputFilterByType DEFLATE application/javascript AddOutputFilterByType DEFLATE application/x-javascript AddOutputFilterByType DEFLATE x-font/otf AddOutputFilterByType DEFLATE image/svg+xml |
2. Apache & PHP & Mysql
Use PHP 7.0
Use Latest Mysql
Use PDO for mysql connection.
3.Check YUI Slow
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:
7. Use Caching Techniques:
8. Use JSON:
Related content
Auriga: Leveling Up for Enterprise Growth!
Auriga’s journey began in 2010 crafting products for India’s