pTutorial

New way to learn PHP

PHP 5 Error Handling and Error tutorial

Coding Style

PHP is very easy-going on syntax. This often results in a programmer writing poor and un-maintainable code.

Programmer use strange variable name like a, xyz etc. Coding style should be consistent and not vary across module.

Some point for logical coding style

  • Documenting code spec with appropriate REM statements.
  • Standard variable name and function name.
  • Approved spacing between modules.
  • Well indented bracing.
.

Note: you should not use strange variable name.

Rigidly following a specified pattern allow script to look almost identical in structure and easy to maintain.

To improve code Spec Readability

Use spaces, tabs and new lines whenever possible to help improve readability. The operator should have a space on either side.

Script 1

<?php 
$max=152;
?>

Script 2

<?php
$max  = 152;
?>

Second script is more readable than the first script. So always use second way to create variable and assign value.

Function parameter should have a space after the comma

<?php
$max=max_function($a,$b,$c);
?>

Script 2

<?php
$max = max_function($a, $b, $c);
?>

Second script is more readable than first script, so always use second way to call the function with parameter.

Use braces even when they are optional

<?php
If($num==num1)
Print"num equal to num1";
?>

Script 2

<?php
If ($num == num1)
{
Print "num equal to num1";
}

?>
.

Note: braces increase the readability of the script.

Meaningful variable and function name

Use meaningful names where appropriate. This means choosing variable names based on the data.

Example

$f
$f_name
$first_name

The third variable is more readable than first and second.

Good variable name

$first_name
$college_name

Average variable name

$f_name
$college_n

bad variable name

$fi
$c
$n

Function

You should use meaningful names if you want to make function, for example, at the time of addition of two numbers you should use addition () not a ().

Commenting code spec

Commenting makes code more readable, so always use comment.

Benefit of commenting

  • Refresh Programmer memory when editing the script.
  • Other Programmer can easily understand the script.

Script 1

<?php 
$num=5 //declare variable and give it value 5
echo $num;
?>

It is a very simple and few lines of code so you think this is the waste of time, but if you will work with large project comment plays very important role.


QUOTES OF THE DAY

The heart of melody can never be put down.

LIKE ON FACEBOOK

+1 ON GOOGLE

SHARE ON TWITTER