Home > php教程 > PHP开发 > The Road to PHP Master (2)

The Road to PHP Master (2)

黄舟
Release: 2016-12-17 09:54:24
Original
1112 people have browsed it

2. Write beautiful code
1. Separate the background program from the front-end program
When writing a PHP program, some codes are used to handle some transactions, such as operating databases, performing mathematical operations, etc., while other codes are just transactions The results of the processing 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 something that can 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 not be able 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 directly using HTML codes 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 is the content of the PHP Master’s Road (2). For more related articles, please pay attention to the PHP Chinese website (www.php.cn)!


Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template