How to Become a PHP master?

Source: Internet
Author: User
Tags php code php programming

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 of all, 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 of the program and reading. 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 more pursuit of stability, structure, very few changes, so should be carefully designed and governed. 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, although the benefits are achieved once and for all, there are some shortcomings 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 we were managing a database, we encapsulated the query () function into a database class, which greatly facilitated code governance 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 section of the code confirms our conclusion. In this code, we use the time test function described earlier.

Use Str-replace instead of Ereg-replace

Programmers who are accustomed to programming with Perl are more willing to use ereg_replace to complete string substitution, because ereg_replace usage in PHP is similar to that of pattern matching in Perl. However, the following code confirms that using Str_replace instead of ereg_replace can greatly improve the speed of your code.

Test Str_replace and ereg_replace running speed

This code tests how fast Str_replace is running

emphasis;?>.

for ($i =0; $i <1000; $i) {
Str_replace (I&GT;, B>, $string). ;
}
?>

This code tests how fast Ereg_replace is running

for ($i =0; $i <1000; $i) {
Ereg_replace (< ([/]*) i>, <1b>, $string).
;
}
?>

Print results

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.