Second, the preparation of beautiful code
1. Separate the background program from the front-end program
When writing a PHP program, some of the code is used to handle transactions, such as manipulating the database, doing math, and so on, while some other code is just the result of the transaction processing, For example, some PHP code that uses the Echo statement to display results in HTML format in a Web browser, and HTML code that embeds PHP programs directly. First, we should clearly distinguish between the two code, the former called the background program, the latter called the front-end program.
Because PHP is an embedded programming language, that is, all of the PHP code can be embedded in the HTML code, which provides a lot of convenience for programming. However, "extremes", if in a longer program in the PHP code and HTML code mixed writing, which will make the program messy, not conducive to the maintenance and reading of the program. So we need to transplant as much of the PHP code in the HTML code as possible into these programs, encapsulate the code into functions in a specialized file, and then use the INCLUDE statement in the HTML code to include the files and call them in the right place.
This approach makes both HTML code and PHP code easy to read, and because the HTML code needs to be constantly updated, this decoupled method ensures that the daemon is not corrupted.
Unlike the front-end program, the background program is more stable, structured, rarely changed, so it should be carefully designed and managed. In fact, in the design of the program, put a lot of time is worthwhile, "now trees, after the cool", in the future design work will be able to easily use the background program now written.
2. Flexible use of Include files
As mentioned earlier, background programs should be arranged in a series of include files. Include files can be loaded dynamically when needed through include statements, or they can be preloaded automatically in php.ini files by using the Auto_prepend_file directive.
If the latter method is used, the benefits will be once and for all, but there are some drawbacks that deserve our attention. The following code shows us how long it takes to parse a large included 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 containing file, the results show that parsing the containing file took 0.6 seconds, for a large web site, this speed is not negligible.
Another disadvantage of using include files is that if one of the statements in a file is wrong, the entire Web site's PHP program will not run. So use it and be careful.
In fact, a little processing of the containing file allows the inclusion file to be parsed only when needed. The following code makes the Abc.inc file parse only when the program needs it:
if (defined (__LIBA_INC)) return;
Define (__liba_inc, 1);
/*
* Code ...
*/
?>
3. Using Object-oriented Programming methods
PHP is also an object-oriented language, object-oriented programming methods are excellent programmers highly respected a software design method, in the PHP programming can give full play to the advantages of object-oriented language, programming objects in the encapsulation. In the previous code, we used an object-oriented approach, for example, when managing a database, we encapsulated the query () function into a database class, which greatly facilitated the management of the code and increased the readability of the program.
Third, the pursuit of the speed of the program, not the speed of programming
In the website construction, the program running speed and the webpage downloading speed are the relations success or failure important factor. As a web programmer, you should pay more attention to how fast your code is running. Several of the methods described below increase the speed of your code to varying degrees.
1. Use embedded HTML code, not PHP's echo statement.
Because PHP is an embedded web programming language, you can embed HTML code and PHP code into each other. But many programmers are concerned about the excessive use of "" embedded PHP code in the HTML code will invoke the PHP interpreter multiple times, thus reducing the speed of PHP code, so prefer to use the PHP echo statement to output HTML code, rather than directly using HTML code. But the exact opposite is true. Each PHP page only calls once PHP interpreter to explain all the PHP code, so, only when needed to embed PHP code, and most of the time directly using the HTML code input results, not only will not reduce the speed of the program, but also because of the reduction of the Echo statement parsing, You can often improve the speed of your code.
The following piece of code proves our conclusion. In this code, we use the time test function described earlier.
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.