What are standard practices in writing PHP code?
Today IDEs are especially powerful with plugins and extensions that can make our codes organized when it comes to indentation. However, these tools still don’t help when the code is difficult to understand.
Coding standards
Standards are generally accepted ways of doing something. In coding there is no single way to code correctly, therefore individuals and companies can define their own ways in which they code amongst themselves. This answer will cover some coding standards.
Generally acceptable standards in PHP
Taking a look at these standards which are well suited for the structure of the PHP language.
Indentation
Although this is now automated, for personal implementation and configuration you should use four spaces and not tabs as they can vary based on the platform you are using.
Code lines
Your lines shouldn’t be too long. The best practice is 80 characters per line at most, whitespaces inclusive, but it is better to keep it as short as possible. Space your code blocks neatly and start new statements on new lines.
Code tags
PHP has a variety of ways to start a script. It is best to always use the <?php opening and ?> closing tags as this tends to be understood clearly by all platforms rather than the shorthand, <?and ?>, whose interpretation can vary with OS platforms.
Commenting
Always use in-code comments to make short comments on code block for a better understanding of your code.
/* */is the C language commenting method for multi line comments//is the style in the C++ language
These two options are the best. Try to avoid the use of the perl commenting pattern: #.
Naming a variable
Your variable names should be lowercase. For compound variable names, use underscore (_) as a separator or use a camelCased name.
You can start the names of GLOBALS which you personally define with “g”. For the PHP GLOBALS, write them in all uppercase. For more on PHP variable naming conventions, check out this answer.
<?php/*I started this script withthe standard andmost safe tag*/// I am commenting with C and C++ styles$myVariable = "is a camelCase style";$you_can_do_so = "another fine way";// below are global constants$_POST;$_GET;$_REQUEST;// All written in uppercase.$g_user_ip; //my own global?>
Functions
Functions in programs are used to make sure you don’t repeat yourself while you code. You can achieve this by ridding your functions of static values to allow them to be reentrant.
When calling functions make sure that:
- You have no space between the function name and the opening parenthesis.
- Immediately after each argument, insert a comma and a space, followed by the next argument. Leave no space between the last argument and the closing parenthesis. Here is a visual example:
$mycall = prefered(arg1, arg2)
Declaration
Try to declare static variables early enough at the start of the script and make the declaration block align properly.
Also, make your function’s declarations follow the BSD style shown below:
function prefered($arg1, $arg2 = '') {
if (condition) {
//code;
}
return $something;
}
Methods and functions length
Try to have your function on a single page of the code. You should be able to view your function at a glance without the need to scroll. This simply implies that your functions should be short and simple.
Control statements
These include if, for, while, switch, etc,. You need to make sure your code has control statements with a single space between the control keyword and the opening parenthesis to make it look different from function calls. Add curly brackets even where you can choose not to. Let’s look at a sample control statement written in a standard patten:
<?phpif ((condition1) && (condition2)) {//action code1;}elseif ((condition3) || (condition4)) {//action code2;}else {default //action code;}//for a switch statementswitch (condition) {case 1://action code1;break;case 2://action code2;break;default:default //action code;break;}?>
There are a good number of ways to write your codes so that it is easy to understand. However, it is best to adhere to the standards outlined in this answer. Some of the other reasons to adhere to these standards are:
- It enhances collaboration
- To reduce time spent on debugging
- For a simple and consistent code structure
- For the sake of industry acceptability