The path of the master of Photoshop, PHP Master (ii)

Source: Internet
Author: User
Tags php programming
Second, write the beautiful code
1. Separate the daemon from the front-end program
When writing a PHP program, some code is used to handle some transactions, such as manipulating databases, doing mathematical operations, and some other code is just the result of transaction processing, For example, some of the PHP code that uses the Echo statement to display the results in HTML format on a Web browser and those that embed directly into a PHP program. First of all, we should clearly distinguish between these two code, the former is called the background program, the latter is 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 brings a lot of convenience to the program writing. However, "extremes meet", if the PHP code and HTML code mixed in a longer program, this will make the program disorganized, not conducive to the maintenance and reading of the program. So we need to try to transplant the PHP code in these programs into the HTML code as much as possible, encapsulate the code into functions in a specialized file, and then use the INCLUDE statement in the HTML code to include the files and invoke them in the appropriate place.
This approach makes both the HTML code and the PHP code easy to read and, on the other hand, because the HTML code needs to be constantly updated, and this separate method ensures that the daemon is not corrupted.
Unlike the front-end program, the background program is more stable, structured, rarely changed, so should be carefully designed and managed. In fact, in the design of the program, invest a lot of time is worth, "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 included files
As mentioned earlier, background programs should be arranged in a series of include files. The include file can be loaded dynamically as needed by the include statement, or it can be pre-loaded automatically in the php.ini file by using the Auto_prepend_file directive.
If we use the latter method, although the benefits have been achieved once and for all, but there are some shortcomings worthy of our attention. The following piece of code shows us how long it takes to parse a large containing 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-row containing file, the result of the run shows that parsing the containing file took 0.6 seconds, for a large website, this speed is not negligible.
Another disadvantage of using include files is that if one statement in a file has an error, the entire Web site's PHP program will not work. So use it and be careful.
In fact, the inclusion file is slightly processed, which means that the containing file can be parsed only 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. Using object-oriented programming methods
PHP is also an object-oriented language, the object-oriented programming method is a very good programmer of a software design method, in PHP programming can give full play to the advantages of object-oriented language, programming objects in the encapsulation. In the preceding code, we used an object-oriented approach, such as when we managed the database, we encapsulated the query () function into the database class, which greatly facilitated the management of the code and increased the readability of the program.
Third, the pursuit of procedural speed, rather than the speed of programming
In the website construction, the program running speed and the webpage download speed are all the important factors of the relationship success or failure. As a web programmer, you should pay more attention to how fast your code is running. Several of the methods described below have increased the speed of code to varying degrees.
1. Use the embedded HTML code instead of the Echo statement in PHP.
Because PHP is an embedded web programming language, you can embed HTML code and PHP code with each other. But many programmers worry that too much of the "" embedded PHP code in the HTML code will invoke the PHP interpreter multiple times, thereby reducing the speed of PHP code, preferring to use the Echo statement of PHP to output HTML code instead of using HTML code directly. But the exact opposite is true. Each PHP page only calls the PHP interpreter to explain all the PHP code, so, only when needed to embed the PHP code, and most of the time the direct use of HTML code input results, not only will not reduce the speed of the program, but also because of the reduction of ECHO statement parsing, It is often possible to increase the speed of code running.
The following piece of code proves our conclusion. In this code, we use the time test function described earlier.

The above introduces the path of the master of Photoshop in PHP (ii), including the Master of Photoshop, the path of the content, I hope the PHP tutorial interested in a friend helpful.

  • Related Article

    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.