Tuesday, January 15, 2013

PHP Coding Tips


  • Use mysql_real_escape_string() function in MySQL Queries.
  • Remember to close the mysql connection at the end of the page using mysql_close().
  • Use comments for better understanding of large programs.
  • Prefer writing column names instead of * in select statement. Prefer writing column names instead of * in select statement.

    Select `fname`,`lname` from `users`
    Instead of
    Select * from `users`


  • Prefer writing comments in PHP rather than HTML because PHP comments are invisible from the browser source code but HTML comments are visible.
  • Avoid using multi-variable assignments unless not needed like:

  •             $var=$_POST[‘user’];
    $user=$_POST[‘user’];

  • Shorten the coding as much as possible. This will optimize the program and make the site faster.
  • Indent properly for the clarity of the program like

  • If($var==1) {
                            do something;
    }
    else { 
                            do something else;
    }

No comments:

Post a Comment