Home > Article > Backend Development > The Road to a Photoshop Master The Road to a PHP Master (2)
2. Write beautiful code
1. Separate the back-end program from the front-end program
When writing a PHP program, some codes are used to process some transactions, such as operating databases, performing mathematical operations, etc., while other codes are just transaction processing The results are displayed, such as some PHP codes that use the echo statement to display the results in HTML format on the Web browser and those HTML codes that are directly embedded in the PHP program. First of all, we should clearly distinguish between these two types of code, calling the former a background program and the latter a front-end program.
Because PHP is an embedded programming language, that is to say, all PHP code can be embedded in HTML code, which brings many conveniences to program writing. However, if things go to extremes, they must be reversed. If you mix PHP code and HTML code in a long program, it will make the program messy and unfavorable for the maintenance and reading of the program. Therefore, we need to transplant the PHP code mixed in the HTML code in these programs as much as possible, encapsulate these codes into functions in special files, and then use the include statement in the HTML code to include these files at the appropriate location. Just call these functions.
On the one hand, this approach makes both the HTML code and the PHP code simple and easy to read. On the other hand, because the HTML code needs to be constantly updated, this separation method can ensure that the background program will not be destroyed.
Unlike front-end programs, back-end programs pursue stability, structure, and few changes, so they should be carefully designed and managed. In fact, it is worthwhile to invest a lot of time when designing desktop programs. "Plant trees now and enjoy the shade later." You will be able to easily use the background programs you write now in future design work.
2. Use include files flexibly
As mentioned before, background programs should be arranged in a series of include files. Included files can be dynamically loaded when needed through the include statement, or they can be automatically loaded in advance by using the auto_prepend_file directive in the php.ini file.
If you use the latter method, although you will get the benefits once and for all, there are also some shortcomings worthy of our attention. The following piece of code shows us how long it takes to parse a large include file:
require(timing.inc);
ss_timing_start();
include(test.inc);
ss_timing_stop();
echo
.ss_timing_current ().
?>
In the above code, test.inc is a 1000-line include file. The running results show that it takes 0.6 seconds to parse this include file. For a large website, this speed is not as fast as It cannot be ignored.
Another disadvantage of using include files is that if an error occurs in a statement in a file, the PHP program of the entire website will be unable to run. So use it very carefully.
In fact, by slightly processing the included file, you can make the included file only parsed when needed. The following code causes the abc.inc file to be parsed only when the program needs it:
if ( defined( __LIBA_INC) ) return;
define( __LIBA_INC, 1 );
/*
* Code...
*/
?> ;
3. Use object-oriented programming method
PHP is also an object-oriented language. Object-oriented programming method is a software design method highly respected by excellent programmers. In PHP programming, object-oriented language can be fully utilized. The advantage is to encapsulate objects in programming. In the previous code, we used an object-oriented method. For example, when managing the database, we encapsulated the query() function into the database class, which greatly facilitated code management and increased the readability of the program.
3. Pursue program speed, not programming speed
In website construction, program running speed and web page download speed are both important factors related to success or failure. As a web programmer, you should pay more attention to the running speed of your code. Several methods introduced below all improve the running speed of the code to varying degrees.
1. Use embedded HTML code instead of PHP’s echo statement.
Because PHP is an embedded Web programming language, HTML code and PHP code can be embedded in each other. However, many programmers are worried that excessive use of "" to embed PHP code in HTML code will call the PHP interpreter multiple times, thereby reducing the running speed of PHP code. Therefore, they would rather use PHP's echo statement to output HTML code instead of directly Use HTML code. But the truth is exactly the opposite. Each PHP page only calls the PHP interpreter once to interpret all PHP codes. Therefore, embedding PHP codes only when needed, and most of the time using HTML codes directly to input results, will not only not reduce the running speed of the program, but also Because the parsing of echo statements is reduced, the running speed of the code can often be improved.
The following piece of code proves our conclusion. In this code, we use the time test function introduced earlier.
The above has introduced the photoshop master's road, the PHP master's road (2), including the photoshop master's road. I hope it will be helpful to friends who are interested in PHP tutorials.