How to write good php code ?

July 21st, 2012 by laeeq | 6 comments

Writing a good code is a art. if a programmer follows some good programming habits then he can be a good programmer. Actually any code you write is likely to spend most its time in maintenance; application maintenance is a large expense. Establishing good coding habits will enhance design factors like modularity, and your code will be easier to understand and, thus, easier and cheaper to maintain. bad coding habits create defects in code and can cause code to be difficult to modify without introducing new defects.

Here in this post we will discuss few good programming habits which will help you avoid defects in your code.

1- Write Structured code

Good PHP code should be structured. PHP’s object oriented programming features give you some especially powerful tools to break up your applications into functions or methods. Also you can comment non-obvious code to make their meaning plain. As much as possible, you should separate frontend HTML/CSS/JavaScript code from the server-side logic of your applications. You can also start on any PHP framework as all follows MVC(Model View Controller) pattern.

2- Write Consistent code

Good PHP code should be consistent. always try to write consistent code by setting rules for the names of variables and functions, adopting standard approaches to recurring tasks like database access and error handling , or simply making sure all of your code is indented the same way, consistency makes your code easier for others to read.

3- Write Portable code

Good PHP code should be portable. you can use php’s existing features such as magic quotes and short tags. Always try to understand your requirement then write your code by adapting PHP features to make code portable and plateform independent.

4- Write Secure code

Good PHP code should be secure. As PHP5 offers excellent performance and flexibility. it leaves important issues like security entirely in the hands of the developer. A deep understanding of major security holes like Cross-Site Scripting (XSS), Cross-Site Request Forgeries (CSRF), code injection vulnerabilities, and character encoding loopholes is essential for a professional PHP developer. By using PHP’s special features and function such as mysql_real_escape_string etc, you can write secure code

5- Documented functions and classes

Code commenting is important part of your code. The comments tell why the functions are doing what they’re doing, which will be much more helpful in the future when the code is being maintained.

6- Avoid Short Tag

Always replaced “short” opening tag with full PHP tag

7- Use single quotes instead of double quotes

Always use single quotes instead of double quotes to avoid the performance hit of PHP searching for variables to interpolate within the strings.

8- Escape string output

Always try to use ENT_QUOTES argument to htmlspecialchars to ensure that single quotes (‘) are also escaped. as it is not necessary to escape but it’s a good habit to get into.

9- Use commas to separate strings ouput

Rather than using the string concatenation operator (.) to pass a single string to the echo statement, the strings to be output by echo are separated by commas for a tiny performance boost.

10- Check post values before output

Always check post values of $_GET['query'] before ouput. isset or empty functions can be used to check variables for null values.

 

You can subscribe to PHPZAG.COM posts by Email

 

Related Topics:

 

 

  1. Antero
    July 23rd, 2012 at 06:58 | #1

    A few simple guidelines that (unfortunately) sometimes even the php language developers seem to forget.

    Ps.Although tip 3 is probably a good idea you say later that short tags should be avoided, also magic quotes have been removed in php 5.4

  2. July 23rd, 2012 at 08:11 | #2

    The “Write Portable Code” section isn’t what I thought it would be. By Portable Code I understand code that will run in all environments, such as Linux and Windows.

    For example, handling the difference for the filesystem separator (/ versus \) between the two.

  3. July 23rd, 2012 at 17:42 | #3

    Actually, Windows knows how to handle / in paths within PHP.

    For example, if your web server root directory is /var/www/, PHP on Windows will treat the root of whatever partition your code lives on as /. So if you have code in C:\var\www\ on a Windows box and code in /var/www on a Linux box, “/var/www” will work within path statements on either.

    That said, for portability, you generally want to have relative paths instead of absolute paths, because you can’t guarantee that every user’s root directory will be /var/www. You can’t even guarantee that your root dir will stay the same if you change hosts.

  4. July 23rd, 2012 at 21:34 | #4

    You’ve made a few reasonable items generally there. We looked on the net to the concern and found the general public goes in addition to together with your site.

  5. bing
    July 23rd, 2012 at 23:36 | #5

    Whats wrong now with the short tag ?
    The previous argument was it may be faded out, and is not oficial, but it is now, and its stated it will remain, so whats the problem ?

  6. July 27th, 2012 at 14:37 | #6

    Hi, i think that i saw you visited my website thus i came to ?return the favor?.I’m trying to find things to enhance my web site!I suppose its ok to use some of your ideas!!

  1. No trackbacks yet.