In many cases, the most valuable feature of PHP may be that its weakest link is its loose syntax. PHP can be widely used because it enables many inexperienced Web developers to create powerful applications without having to think too much about planning, coherence, and documentation.
Unfortunately, many PHP source codes are bloated and difficult to read or even maintain. I deeply understand this, because I have already written a lot of such code.
To avoid the above situation and many other situations. A lot of core PHP developers and group members came together and started PEAR, a project focusing on adding PHP extensions and plug-in repositories. It is unknown till now that documents and other things from the PEAR project are rarely and hard to understand. This article attempts to tell developers what they (PEAR group) have done.
An important factor determining code maintainability is the code format and comments. All code of a project should be organized throughout the process. I am very persistent in the Construction of code libraries. I think programmers should do the same.
(1) indent
All the code of the developer should be written in indent mode. This is the most basic measure to improve code readability. Even if you do not comment on your code, indentation is very helpful for others to understand your code.
For 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 four spaces for indentation instead of tabs. I personally do not agree with this idea. I think I will continue to use the tab key. I think using tabs can make the file smaller than multiple spaces. Smaller files can be explained, uploaded, and downloaded more quickly. The use of tab is a big one, that is, when you watch other people's code, you can set the number of spaces for the tab key by yourself. I usually use the tab key with eight spaces, but recently I changed it to four spaces. I just liked to reformat the code.
(2) control structure
This depends largely on your taste. I can still see that many of the control structure codes do not contain branch statements, resulting in poor readability. IF you use IF statements without branches, not only will the readability become worse, when others modify your program, many bugs may occur. See the following example:
Bad example:
If ($ a = 1) echo 'a was equal to 1 ';
This is very hard to identify. It works normally, but the code is not appreciated by others except you.
Examples of improvements:
If ($ a = 1)
Echo 'a was equal to 1 ';
At least this code can be understood, but it still does not have good maintainability. What if I want an additional event to happen when $ a = 1, or if I want to add a branch? If later programmers forget to add the keyword "Large arc" or "else", a bug will occur in the program.
Perfect example
If ($ a = 1) & ($ B = 2 )){
Echo 'a was equal to 1 ';
// You can easily add other codes.
} Elseif ($ a = 1) & ($ B = 3 )){
// Other operations
}
Note that there is a space after if and elseif. This separates this statement from the function call. In addition, although there are no statements in the elseif Execution Section, only comments exist, on the surface, it seems redundant, but it gives a very convenient prompt to the programmers who want to maintain the program in the future, and it is very helpful to add functions.