Standard writing code in PHP

Each company has different encryption standards based on their practical standards. Encryption standards are necessary because there may be many developers working on different modules, so if they start inventing their own standards, then the source code will become very unmanageable. and it will become difficult to maintain source code in the future.

Each company will have different code standards based on their work practices. Code standards are necessary because there may be many programmers working on different modules, so if they start inventing their own standards, then the source code will become a difficult, difficult stack. manage and difficult to maintain source code in the future.

Here are the reasons why code code must be used:

  1. Programmers working with you must understand the code you created. A common code standard acts as a blueprint for all members participating in that project in the team.
  2. Simplicity and clarity can be achieved by proper code will help you reduce common mistakes.
  3. Not once written code is done, sometimes you will have to see, edit your code, with the standard code agreed before, you will easily understand the code you have written, the view / edit also more advantageous.
  4. Its industry standards follow a specific standard to get better quality in software.

Here are some guidelines you should follow when writing code in PHP programming:

Indentation and line length - Create indentation by pressing 4 times the space bar (space bar) and do not use any tab keys because different computers use different settings for the tab key. For the length of the code line, you should keep the line of code about 75-85 characters long, which makes your code more readable.

Control structure - It includes if, for, while, switch , . Between these keywords and the opening parenthesis, there should be a space (press once) to distinguish them from function calls. You are strongly encouraged to always use curly braces whenever possible.

For example

 if ((điều_kiện 1 ) || (điều_kiện 2 )) { hành động cần thực thi 1 ; } elseif ((điều_kiện 3 ) && (điều_kiện 4 )) { hành động cần thực thi 2 ; } else { default hành động cần thực thi mặc định ; } 

You can write switch commands as follows:

 switch (thing_kale) { 
case 1:
Action to execute
break;

case 2:
Action to execute
break;

default:
default action
break;
}

Call to function - When calling the function, between the function name, the opening and first parentheses should be written immediately, without spaces; Add a space between a comma and each parameter, and there is no space between the last parameter, closing brackets and semicolons. Here's an example:

 $ var = foo ($ bar, $ baz, $ quux); 

Function definition - Declare the function by "BSD / Allman style".

 function fooFunction ($ arg1, $ arg2 = '') { 
if (thing_kien) {
command to execute
}
return $ val;
}

Comment - Comment in C (/ /) language and in C ++ (//) standard both OK. The use of Perl / shell (#) style comments is not recommended.

PHP code card - Always use To determine the scope of PHP code, do not use abbreviations .

Variable name:

  1. Use all is lowercase.
  2. Use underscores (_) as a delimiter.
  3. Global variables (global) should be added before a "g".
  4. Global constants should be uppercase with the separator character underscores (_).
  5. Static variables should be added before an "s".

Creating Reentrant functions - Functions should not contain static variables but prevent a function from being reentrant.

Align declarative blocks - The declaration block should be aligned.

One command per line - There should only be one statement per line, unless the statements are very closely related.

Short functions and methods - Methods should be limited to one code page.

There may be many guidelines, other rules are mentioned when writing code in PHP. Ideally, all these rules and guidelines should be consistent throughout the code process and this only happens when you follow certain code standards. You can have your own standard if you like to make a difference.

Follow tutorialspoint

Previous article: Upload File in PHP

Next article: Variable predefined in PHP

4 ★ | 1 Vote