PHP expert path (2)

Source: Internet
Author: User

2. write beautiful code
1. Separate background programs from front-end programs
When writing a PHP program, some code is used to process some transactions, such as database operations and mathematical operations. Other code is only displayed as the result of transaction processing, for example, some PHP code that uses echo statements to display results in HTML format on a Web browser and the HTML code that directly embeds the PHP program. First, we should clearly distinguish the two types of code, the former is called the background program, and the latter is called the front-end program.
Because PHP is an embedded programming language, that is to say, all PHP code can be embedded into HTML code, which brings a lot of convenience for programming. However, if PHP code and HTML code are mixed in a long program, this will make the program messy, not conducive to program maintenance and reading. Therefore, we need to try to port the PHP code in these programs mixed with HTML code, and encapsulate the code into a function in a special file, then, use the include statement in the HTML code to include these files, and call these functions at the appropriate location.
On the one hand, this method makes HTML code and PHP code easy to read, and on the other hand, because HTML code needs to be constantly updated, and this separation method can ensure that the background program will not be destroyed.
Different from front-end programs, background programs are more stable, structured, and rarely changed. Therefore, they should be carefully designed and managed. In fact, it is worthwhile to invest a lot of time in the design of a program. "planting trees now and enjoying the cold in the future" will make it easy to use the background program compiled in the future design work.
2. flexible use of include files
As mentioned above, background programs should be arranged in a series of include files. The include statement can be used to dynamically load the included files as needed, or the auto_prepend_file command can be used in the php. ini file to automatically load the files in advance.
If the latter method is used, although the benefits have been achieved once and for all, there are also some shortcomings that deserve our attention. The following code shows that it takes some time to parse a large file inclusion:
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-row file. It takes 0.6 seconds to parse the file. For a large website, this speed is not negligible.
Another disadvantage of using include files is that if a statement in a file is incorrect, the PHP program of the entire website cannot run. So be careful when using it.
In fact, the inclusion file can be parsed only when necessary. The following code parses the abc. inc file 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. The object-oriented programming method is a software design method highly praised by excellent programmers, in PHP programming, we can give full play to the advantages of object-oriented language and encapsulate the objects in programming. In the previous code, we used an object-oriented method. For example, when managing a database, we encapsulated the query () function into the database class, which greatly facilitates code management, added the readability of the program.
3. Pursuing program speed, not programming speed
During website construction, the program running speed and website download speed are important factors that affect the success or failure of the website. As a Web programmer, you should pay more attention to the code running speed. The methods described below increase the code running speed to varying degrees.
1. Use embedded HTML code instead of PHP echo statements.
Because PHP is an embedded Web programming language, HTML code and PHP code can be embedded into each other. However, many programmers are worried that the PHP interpreter will be called multiple times when "" is used too much in HTML code to reduce the running speed of PHP code, therefore, we would rather use PHP echo statements to output HTML code rather than directly using HTML code. But the opposite is true. Every PHP page only calls the PHP interpreter once to explain all the PHP code. Therefore, the PHP code is embedded only when necessary, and most of the time the HTML code is used to input the result directly, this will not only reduce the program running speed, but also reduce the resolution of echo statements, which can often improve the code running speed.
The following code proves our conclusion. In this Code, we used the time test function described above.

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.