index act articles resource 9 ext iframe

Coding Guidelines

This article explains a few of the PHP coding guidlies that is recommended to to follow, including a few tips and tricks. Coding guidelines differ between coder and is no correct or incorrect way.




Coding Guidelines
1431 Views
Definining variables and functions


When naming variables, keep them all lower case. If a variable name has multiple words, separate them with underscores – like so:

Code: [hide]
  1. $variable_name 

If you use a piece of code which is used many times throughout your code, it is better to place it in a function. This will reduce the amount of code in your scripts and will most likely increase performance. Remember if you want to pass variables to a function defined outside of the function remember to declare them global. IE –

Code: [hide]
  1. global $variable_name; 

As wtih variable naming, function names should be declared using the same conventions as I explained above. IE –

Code: [hide]
  1. function function_name() 






Quotes and spacing


When assigning a variable a value, use spaces either side of the equal sign (=). IE –

Code: [hide]
  1. $variable_name = ‘Jack’; 

When assigning a variable to a number, there is no need for quotes so simply use

Code: [hide]
  1. $variable_name = 0; 

When incrementing a number, increment it like so –

Code: [hide]
  1. $variable_name++; 

In this case there is no space as we are not using the equals sign to assign it a new value.




When echoing a variable, there is no need to echo it like so –

Code: [hide]
  1. echo “$variable_name”; 

Even thought the variable is parsed inside double quotes, double quotes are parsed slowly, so simply use

Code: [hide]
  1. echo $variable_name; 

Bearing the speed of double quotes, if you wanting to echo a static string, use single quotes. IE –

Code: [hide]
  1. echo ‘Hello world’; 




When using functions such as if() or while() etc… leave a space between the function name and the open bracket. This makes it clearer to other programmers (including yourself) exactly what function you are using. IE –

Code: [hide]
  1. if ($variable_name == 1) 

Specific technique:If you are using an if() statement, which is consists of a simple if…else consider using a ternary operator. Take the following code:

Code: [hide]
  1. if ($variable_name == 1)
     
  2. {
     
  3.    $output = ‘You got the correct answer!’;
     
  4. }
     
  5. else
     
  6. {
     
  7.    $output = ‘You answered incorrectly’;
     
  8. } 

For a very small if() statement, it takes up a lot of room. This can easily be changed to

Code: [hide]
  1. $output = ($variable_name == 1) ? ‘You got the correct answer!’ : ‘You answered incorrectly’; 

This takes up a lot less room and is more tidier (in a simplistic case).






Braces


There are many ways to lay out braces, but the most clearest way is place braces on new lines. Ie –

Code: [hide]
  1. if (!empty($variable_name))
     
  2. {
     
  3.    foreach ($variable_name_two as $key => $value)
     
  4.    {
     
  5.        echo $value . ‘
    ‘ . “ 
  6. “;
     
  7.    }
     
  8. } 






Optimization


Optimize yours scripts where possible. Especially in loops as in each iteration the conditon is executed.




When using echo() to show some information, consider different ways. IE –

Code: [hide]
  1. echo “Your IP: {$_SERVER[‘REMOTE_ADDR’]}”;
     
  2. echo ‘Your IP: ‘ . $_SERVER[‘REMOTE_ADDR’];
     
  3. echo ‘Your IP: ‘, $_SERVER[’REMOTE_ADDR’]; 

In the above code, double quote marks (“) are parsed slowly, so where possible try not to use them (with exception of “

” etc.. as these dont work in single quotes). The third example is fast as parameters of the string are separated by a comma and it is not using double quotes.

Coding Guidelines