Today's Question:  What does your personal desk look like?        GIVE A SHOUT

Beauty of code : How to write graceful PHP code

  Peter        2012-08-26 12:23:03       15,089        0    

Writing good code is an art. In order to achieve this, it is necessary to develop good programming habits at the beginning. Good programming habits not only contributes to the early project design (modular),but also allows you to the code easier to understand, so that the maintenance of the code is easier. Bad programming habits will result in more code bugs, and will make future maintenance work difficult.


We introduce some good programming habits taking PHP as example. Hope this will help you.


1. Planning code structure

Excellent PHP code should have a clear structure. PHP object-oriented features allow the programmer to decompose the application as functions or methods. If the code is obscure, you can also add comments to the code and make functions of the codes easy to understand. When coding, keep front-end code (HTML / CSS / JavaScript) separate from server side codes, or you can use a PHP framework that follows the MVC pattern to build your application.


2  Standardize coding style

Excellent PHP code should have a unified style. For example, variables and functions should have the same naming convention and common interface (such as database access, error handling), or maintain a regular code indentation, All these coding habits can let someone else read your code much easier.


3. Portability

Excellent PHP code should be portable. Programmers should learn to use PHP's existing features (such as magic quotes and short tags, etc.), you should understand the product requirements, adapt to the characteristics of the PHP. Ensuring PHP code portabile and cross-platform compatible.


4 Code security

PHP code should be secure. PHP5 has exceptional features and flexibility, but the safety of applications often rests in the hands of programmers. As a professional PHP developer, you should have some in-depth understanding of security vulnerabilities, some common security vulnerabilities including cross-site scripting attacks (XSS), cross-site request forgery (CSRF), code injection vulnerabilities and character encoding vulnerability. PHP specific features and functions (such as mysql_real_escape_string, etc.) can be used to write secure code.

5 Add comments

Code comments are important parts of the code, they explain the purpose of functions. Comments will provide useful help in the future maintenance of the code.


6. Avoid shorthand tag

They're not recommended because it's a PITA if you ever have to move your code to a server where it's not supported (and you can't enable it). As you say, lots of shared hosts do support shorttags but "lots" isn't all of them. If you want to share your scripts, it's best to use the full syntax.

7 Using single quotes instead of double quotes

Because PHP will search variables in double quotes.Whenever you are not using variables inside a string it's good practice to just use single quotes so PHP doesn't have to parse the string.


8. Escape output

Use ENT_QUOTES in function htmlspecialchars, to guarantee single quotes (') can also be escaped. Although not required to do so, this is a good habit.


9 Use a comma-separated string output

String concatenation operator (.) can pass a single string to echo statement output,echo using commas is marginally faster than echo with concatenation


10 Check parameters before output

Checking value of parameter such as $ _GET ['query'] before output. Use isset or empty function to check the value of the variable is empty.

PHP  GOOD CODE 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  0 COMMENT


No comment for this article.