PHP Programming Style _php Tutorial

Source: Internet
Author: User
Tags pear php programming php source code
Original Author: Tim Perdue
Source: Www.phpbuilder.net
In many cases, PHP's most valuable feature may be its weakest link is its grammatical loosely. PHP is so widely used because it enables many inexperienced web developers to make powerful applications without having to think too much about planning, coherence, and documentation.
Unfortunately, it is the above characteristics, is a lot of PHP source code is very bloated, difficult to read and even can not be maintained. I deeply appreciate this, because I have written a lot of such code. ;-)
In order to avoid the above situation and a lot of other situations occur. Many of the core PHP developers and community members came together to start pear, a project that focuses on adding PHP extensions and plugin repositories. Until now, the documents and other things from the Pear project were little and incomprehensible, and the article tried to tell the developers what they were doing (the Pear group).
The important factor that determines the maintainability of code is the format and comment of the code. All code for a project should be organized in a consistent way. I'm very insistent on building code libraries, and I think programmers should do the same.
(1) Indent
Developer-owned code should be written exactly as indented. This is the most basic measure to improve the readability of code. Even if you don't annotate your code, indentation is for other people to read
Your code is also a great help.
For example, the following example:
while ($x < $z) {
if ($a = = 1) {
echo A was equal to 1;
} else {
if ($b = = 2) {
Do something
} else {
Do something else
}
}
}
The pear Draft standard requires the use of 4 spaces to indent and then not to use tab. I personally do not agree with this view, I think I will continue to use the TAB key. I think using tab more than a few spaces can make the file smaller. Smaller files can be interpreted faster, uploaded, downloaded, and so on. Using tab also has a big bit, that is, when you look at other people's code, you can set the TAB key to the number of spaces. I usually use 8 space-length tab key settings, but recently replaced by 4 space-length, hehe, I call him the code of the reformatting, just a personal hobby.

(2) Control structure
This depends to a large extent on individual tastes. I can still see a lot of control structure code without branching statements resulting in very poor readability, if you use if statements without branching, not only the readability of the poor, when others modify your program, but also cause a lot of bugs. Take a look at the following example:
A bad example:
if ($a = = 1) echo A is equal to 1;
This is very difficult to identify. It works, but no one else appreciates the code except you.
There are examples of improvements:
if ($a = = 1)
echo A was equal to 1;
Now at least this code can be read, but it's still not good maintainability. What if I want an additional event to occur when I'm $a==1, or do I need to add a branch? If later programmers forget to add the braces or else keyword, a bug will appear in the program.
The perfect example
if ($a = = 1) && ($b ==2)) {
echo A was equal to 1;
It's easy to add additional code
} elseif (($a = = 1) && ($b ==3)) {
Other operations
}
Note the spaces following the IF and ElseIf, which distinguishes this statement from the function call, and, although there are no statements in the ElseIf's execution segment, only comments, which appear superfluous on the surface, give a very handy hint to the programmers who maintain the program later, and are very helpful for adding functionality.
Source: House Orso near Network

http://www.bkjia.com/PHPjc/531830.html www.bkjia.com true http://www.bkjia.com/PHPjc/531830.html techarticle Original Author: Tim Perdue Source: Www.phpbuilder.net In many cases the most valuable feature of PHP may be that its weakest link is its grammatical looseness. PHP can be so widely made ...

  • 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.