Home  >  Article  >  Backend Development  >  The Road to PHP Master (2)_PHP Tutorial

The Road to PHP Master (2)_PHP Tutorial

WBOY
WBOYOriginal
2016-07-21 16:04:44801browse

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 Then only the results of transaction 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 PHP programs. 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 an include file with 1000 lines. The running results show that parsing This included file took 0.6 seconds, which is not negligible speed for a large website.
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 methods
PHP is also an object-oriented language. Object-oriented programming methods are highly respected by excellent programmers. A software design method that can give full play to the advantages of object-oriented languages ​​in PHP programming and 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, so 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, the PHP code is only embedded when needed, and most of the time, the HTML code is used directly to input the results. Not only will it not reduce the running speed of the program, but it will 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.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/315882.htmlTechArticle2. Write beautiful code 1. Separate background programs from front-end programs. When writing PHP programs, some codes are Used to handle some transactions, such as operating databases, performing mathematical operations, etc., and...
Statement:
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