10 PHP tips for web developer

PHP widely used for web development. More than 80% websites on internet are built using php. In this article i’ll tell you 10 most useful tips for php.

1. Always Use long open tags

When you declare a php script always use long tags <?php instead of short tags <?. Because php configuration on most servers do not allow short tags hence your code will not work.

2. Use error reporting

Turn on error reporting on your development server this will help you to find errors in your php code. To turn on error reporting on your server edit php configuration file php.ini and set following variables

error_reporting = E_ALL
display_errors = On

If you want to enable error reporting in just current script then add following two lines on top of your php script


3. Use echo instead of print

Echo is slightly faster than print and echo can take multiple parameter while print takes only one. So always try to use echo instead of print function.

4. Know the Difference Between Single and Double Quotes

Single quoted strings will display things almost completely “as is.” Variables and most escape sequences will not be interpreted. The exception is that to display a literal single quote, you can escape it with a back slash \', and to display a back slash, you can escape it with another backslash \\

Double quote strings will display a host of escaped characters (including some regexps), and variables in the strings will be evaluated. An important point here is that you can use curly braces to isolate the name of the variable you want evaluated. For example let’s say you have the variable $type and you what to echo "The $types are" That will look for the variable $types. To get around this use echo "The {$type}s are" You can put the left brace before or after the dollar sign.


echo 'my name is $name'; // my name is $name
echo "my name is $name"; //my name is sunny

5. Use inline comment

Always use inline comments to annotate your code. It will help to understand the code so anyone can easily modify it in future. Read more about comment in php

6. Don’t Store Passwords as Plain text

Most php beginners  store passwords as plain text in database which can be viewed in database that is not safe. So always use md5 or sha1 to encrypt sensitive data like passwords before storing it in database.



7. Filter and validate form data

Never trust user as they can input any arbitrary code in form which may leads to XSS (cross-site scripting) attack or sql injection. To protect your script from such vulnerability always validate and filter form data before processing.

8. Use cache in heavy Script

Most heavy php code takes much time to generate output which casts bad impact on website. To speedup your script always use caching in php script. Here are few caching scripts for php

alternatively you can also enable MySQL query cache to speedup MySQL queries. To enable it add following 3 lines in MySQL configuration file (my.conf)

query_cache_size = 268435456
query_cache_type = 1
query_cache_limit = 1048576

9. Use Captcha verification in forms

Hackers may use bots to spam your website by submitting  arbitrary data in forms like comment and contact forms. To protect your website always use captcha verification in form. Here is simple php captcha script.

10. Try an IDE

IDE’s (Integrated Development Environments) are helpful tools for any developer. A good IDE has features like

  • Syntax highlighting
  • Code auto-complete
  • Debugger
  • Ftp support

Here is list of my favourite IDE’s

  1. Dreamweaver
  2. Netbeans
  3. Eclipse
  4. PhpStrom
  5. Geany (Linux only)

Liked It? Get Free updates in your Email

Delivered by feedburner

Leave a Reply

Your email address will not be published. Required fields are marked *