Standard writing code in PHP

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

May be interested

  • The 'battlefield code' and job opportunities for youThe 'battlefield code' and job opportunities for you
    follow quantrimang with a list of websites that host 'useful playgrounds' for the coder to hunt for bonuses or get yourself a great job.
  • 4 ways to write multi-threaded code in Java4 ways to write multi-threaded code in Java
    multithreading is a method of writing code to execute multiple tasks in parallel. java offers great support for multi-threaded code writing right from java 1.0 version. recent improvements in java have increased the number of ways that code is structured to combine multiple threads in java programs.
  • How to Create a Network Application in JavaHow to Create a Network Application in Java
    writing code that executes on a certain device is very satisfying. but, writing code that executes on several devices that communicate with each other is simply life-affirming. this article will teach you how to connect and exchange...
  • Warcraft 3 code - Summary of code, cheat, Warcraft 3 standardWarcraft 3 code - Summary of code, cheat, Warcraft 3 standard
    if you are new to playing, you can use the commands (cheat) or warcarft 3 code to ask for money, immortality, revive right in the game..vv. to experience the game more easily. the following article of thuthuatphanmem.vn will summarize for you all b
  • 10 tips for using Codepen for beginners10 tips for using Codepen for beginners
    codepen is an indispensable editor of hmtl, css and javascript for front-end programmers. if you do not know it, these 10 simple tips will help you get familiar with codepen very easily.
  • What is ASCII? The ASCII code is standard and completeWhat is ASCII? The ASCII code is standard and complete
    if you are just beginning to learn about ascii code and ascii code table, please refer to the article below. the article provides the concept of ascii code and ascii charset and most complete for your reference and learning. enough to study and work.
  • 10 best phone apps to learn to code10 best phone apps to learn to code
    in this article, tipsmake lists a list of the top 10 best coding apps for mobile.
  • 7 best AI writing tools today7 best AI writing tools today
    whether you struggle with writing or hate writing, ai writing tools can help. not only will they save you time writing the first draft, but they will also give you lots of creative ideas.
  • 7 simple ways to practice writing skills7 simple ways to practice writing skills
    if you want to write fast, write well, so that anyone who read it will have to love, there is no other way to practice. so how to practice writing skills to be most effective?
  • 10 principles to help quickly post top Google10 principles to help quickly post top Google
    in today's era, whenever we need to search for information, we often search for google and there is nothing better if your website is located on the first page of search results. however, in order to do this, the content of the articles on your website must meet seo standards, which satisfies two conditions: seo standard and useful.