SoFunction
Updated on 2025-03-09

Summary of basic optimization methods for PHP websites

1. Use GZip

Add the following code at the top of each PHP page:

<?php ob_start("ob_gzhandler");?>

After using this code, the server compresses all code that needs to be transferred to the client and decompresses it in the browser, thus speeding up the website. This feature can also save traffic on the website space.

2. Don't abuse Javascript and Ajax

Use Javascript and Ajax only when needed, and never abuse them. Some sites use too many unnecessary Ajax animations, or use Ajax to load useless parts. This way, Javascript files will become large, but there are actually many other solutions to implement these functions.

3. Images, header files and HTTP requests

This is the most critical part of this article.Web pageThe more pictures, external files, and CSS-style files are referenced, the slower the web page will load. Take some time to narrow down the image files and other external files so that they can be loaded faster. In addition, an HTTP request will be generated every time the image and external files are loaded, which will definitely delay the loading time. You can use the following methods to compress web page files, JS files, and CSS style files:

Web pageFile: Use GZip, see the first item
JS file:/jsmin/This website can effectively reduce the size of JS files
CSS File://main/csscompressor/This website can reduce the size of CSS files

4. Limit the number of MySQL queries

Every request to the database will makeWeb pageThe loading speed is slowed down. Web developers allow this to be difficult to control, but it can be optimized in some details. For example, when selecting a database record, do not use the following code:

SELECT * FROM database

Instead use:

SELECT id, name, date, author, etc, blah, blah FROM database

This consumes less query time and reduces the load on the server.

5、.phpExtension

Some people think of saving JS files as .phpAnd saving the CSS file as will reduce the loading time, but I didn't notice the difference. Of course, if your website gets slower, you can try this method. Of course, you need to use include() to load these files in each PHP file.

6、/

Go to this website to check your own website, it will give you some information about how to do the websiteoptimizationsuggestions. I build a new one every timeWeb pageThis function will be used whenever.