Last Updated : March, 2019

PHP being widely used for web development and applications. More than 90% of websites on internet are built using php. In this article I will Recommended you these tips that will improve your skills.

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.

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

ini_set('display_errors',1);

error_reporting(-1);

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.

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.

Example


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

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

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.

example

$passHash=sha1("password");

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.

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

  1. Memcached
  2. XCache
  3. Zend Cache
  4. eAccelerator
  5. Cache_Lite

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

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.

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 favorite IDE’s

  1. Dreamweaver
  2. Netbeans
  3. Eclipse
  4. PhpStrom
  5. VScode
  6. VM