To improve work efficiency,To ensure the effectiveness and rationality of development,And maximize the readability and reusability of the program code,Improve communication efficiency,Need a code editing specification.

Why do we develop our own coding standards?

When we write code,A good coding convention,For us it can have many unintended effects.There are at least the following benefits:

1. Improve our coding efficiency.The uniform code is convenient for us to copy and paste!

2. Improve the readability of the code.

3. Show us professional.Others see our code,Found that the entire code writing process is uniform.Instantly push the grid up!

4. Facilitate teamwork.Everyone uses the same specification,This eliminates all kinds of writing,The same coordination!

The coding specification consists of two blocks,Code and comment specifications

In fact, the php script we write is actually composed of two big blocks.That is to write the code, comment on the code! Different frameworks,Even different companies,Will be different in this regard,Here is just the wordsJust a summary of your writing specifications! Hope to inspire other friends

1.Code writing specifications

Name of the folder:

Folders use the same lowercase letters.Such as the folder that holds the controller,Just name controller

File name:

If it is a class file,Then the file name should be consistent with the same name,Use big hump uniformly.Such as session.class.php, the corresponding class name is session,

If it is a normal tool script,Then use the small hump uniformly,Like common.php

Naming of class names:

Class names use the same big hump,cookie class

Method name:

Uniform use of small hump,The form of verb + rank is generally used to describe the function of the method,If sendmessage, send a text message.

In object-oriented,Following the same rules,But there are differences in individual places:

public getusername ()
protected _gettotalamount ()
private _setblanceamount ()

Variable naming convention:

Regarding variables,We need to say a few more points:

1. Whether in object-oriented or non-object-oriented syntax,Variables use little hump,For example:$workyears.

But it's different in object-oriented,When the public, protected, private, protected, or private propertiesPreceded by _ as a difference

public $startposition=1;
protected $_salaryamount=1000;
private $_sex="formale";

2. If it is constant,Use uppercase uniformly,Use underscores in the middle to split.

define ("current_script", "index.php");
const transaction_type="income";

3, global variables,Using big hump,Prefix with _ to capitalize all words.Because it's important to know the scope of a variable,So local variables and global variables should be clearly separated!




Regarding encoding indentation,We use tab indentation uniformly! Some people may ask why not use space indentation?

the reason is simple,Most editors support how many spaces a tab is equal to,And the use of spaces does not have to be adjusted!

calculating signs

All binary operators,Should use spaces before and after


Common process statement planning

We agreed,The curly braces for all process statements are on a separate line.Why:If you encounter more complex business logic,There will be a lot of nesting in curly braces,In this way we will confuse the corresponding curly braces!

Branch statements

if ($age>= 18&&$age<= 30)
  echo "young man";
else if ($age>30&&$age<= 60)
  echo "middle aged";
  echo "old man";
//The following piece of code master we have a question,In an if statement, even if you don't need curly brackets,The curly braces are also written.
if ($age>60)
  echo "i am very old";
switch ($status)
  case "forbiden":
    echo "login forbidden";
  case "normal":
    echo "login in";
    echo "status is wrong":

2, loop statements

while ($condition)
  statesments ...;
foreach ($arraylist as $arraykey =>$arrayitem)
  states ...;
  statements ...;
while ($condition)
for ($start;condition;changenumber)
  statements ...;

2.Writing specifications for comments

Many people say that good code does not require comments.In fact, personally I think this is a nonsense sentence (maybe he is right,Unless the entire team is alone,He did everything,Don't look at other people's code).

My personal point is:write more notes,Whether for the rest of the team,Still very friendly to myself!

According to personal experience,Annotations have at least the following functions:

1. Conducive to improving the readability of the code,After all, reading your comments is easier than reading your code!

2, is conducive to planning your own code layout! The reason to say so,Because it is related to the type of code comments."It's good for the layout of the code"I just do n’t understand.We need real examples for support!

3. Since our annotation specifications are in accordance with the requirements of phpdocumentor, through this tool,You can also generate a general description of the code,Equivalent to an instruction manual! g

Types of code comments

Block comments

Block comment, I think it is mainly used in three places.A description of a php script, a description of a large functional module, and when you cannot finish commenting in one line,Should also be placed in block comments

Line comments

Line comments, personally think he works with block comments! Generally used to describe the specific details of a large functional module!

Practical case

About the specific usage details of phpdocumentor syntax,Not much to say here,It's not clear on the official website

From the above example, we can see how the layout of the code is going.But we still need to explore it slowly in practice

Attached below are some PHP programming specifications,For your reference

I. File marking:

1. All php files are coded with full php tags. Short tags are not recommended (short tags are easy to confuse with XML. PHP does not support short tags by default since 5.4).

2. For PHP-only code files,It is recommended to omit "?>" at the end. This is to prevent extra spaces or other characters from affecting the code.

File and directory naming

1. The program file name and directory name are named in English.No pinyin and meaningless letters,Only letters, numbers, underscores, and underscore characters are allowed, and must end with ".php" (except for template files). Use camel case between multiple words.

Example://Class uniformly adopted:demotest.class.php

//The interface is unified:demotest.interface.php

//Others in their own way:demotest. {Style} .php

Third, the file directory structure

The standardized directory structure facilitates collaborative development and later maintenance.

-App //standalone application

——Class //a single class file,Shared class file

--Conf/inc //Configuration file or directory

——Data //data file or directory

--Doc //program related documentation

——Htdocs //document_root

——Images //All image file storage paths

——Css //css file

--Js //js file

--Lib //shared class library

--Template //template file

——Tmp //Temporary file directory

--Cache //cache file

--Session //session file

——Template_c //Compiled template file


---- upload //upload file

——Manage //Background management file directory

Fourth, the naming convention

1. Variable naming:Variables in PHP are case sensitive,A valid variable name begins with a number, letter, or underscore.Followed by any number of letters, numbers, underscores.

a) The whole program is named by camel case,Start with lowercase letters,It also makes sense to name them.(Function displayname ())

b) PHP global variables have "_" on both sides of the key value, and the name is named by camel case.($_Global ["_ begintime_"])

c) Camel naming is adopted for ordinary variables as a whole.It is recommended to prefix the type with a variable.Indeterminate types begin with an uppercase character.

d) the function name should be as meaningful as possible,Try to abbreviate.

2. Class and interface naming:

a) Start with a capital letter.

b) variable names consisting of multiple words,No spaces between words,Capitalize the first letter of each word.

c) The class name is consistent with the class file name.

d) All class names in the program are unique.

e) Abstract classes should begin with abstract.

Interface naming rules:

i) adopt the same naming rules as classes,But the "i" character is added before the name to indicate the interface.

ii) Try to be consistent with the class name that implements it.

3. Database naming:In database related naming,Never capitalize.

a) Table names use lowercase letters.

b) The table name uses the same prefix and the prefix cannot be empty.

c) For a table name composed of multiple words,Use "_" intervals.

d) Table field naming rules.

i) Use all lowercase letters.

ii) Multiple words are not separated by underscores.

iii) Prefix common fields with the first letter of the table name.

iv) Avoid keywords and reserved words.

Five, annotation specifications

1. Program comments:written in front of the code instead of behind,A single line of code is written at the end of the code as usual;Large comments are written in/** /, usually at the top of a file or function.Use "//" inside the code;comments should not be too much;Code comments should describe why rather than what,Provide the most important information to code readers.

2. File comments:File comments are generally placed at the top of the file.Including the description of the program, author, project name, file name, date and time, version information, important instructions for use (class call,Notes, etc.). To change a version, modify the version number.And add mofify comments.

3. Class and interface annotations:According to general conventions,A file contains only one class.

4. Method and function comments:Method and function comments are written first,It is often necessary to indicate the primary visibility of the information, the parameter types, and the return value types.


* Connect to the database

* @param string $dbhost database server address

* @param string $dbuser database user name

* @param string $dbpwd database password

* /

Six, code style

1. Indentation and spaces:use 4 spaces for indentation,Do not use the tab key;when assigning variables,Leave spaces around the equal sign.($Url="$_get [" url "]";)

2. Statement line break:Try to ensure that the program statement is one line at a time;Try not to make one line of code too long,Less than 80 characters;If one line of code is too long,Please use ". =" To break the connection;When executing the SQL statement operation of the database,Try not to write sql statements inside functions, but define sql statements with variables first, then call the defined variables in the function that performs the operation.

3. Better habits:Use the methods listed below in your code,Can make the code more elegant.

1):use existing constants in php,Don't define it yourself.

Example://line feed

echo $msg. "\ r \ n";

echo $msg, php_eol;

php_eol is a predefined constant in php,Indicates the end of a line,Depending on the system used,More portable with php_eol code

2):Use a comma as a connector in echo,More beautiful than using "." For the connector code.

3):Single quotes are more efficient than double quotes,But the two are different in use.Learn to use the printf function.


echo "each". $scholl. "about" .floor ($avg). "students";


$format="Each%s is greater than $d students";

printf ($format, $school, $avg);

4):Detailed notes

5):Do not abuse syntactic sugar,Syntactic sugar is a hidden rule in language.That is, there is no universally representative grammar.

  • Previous JavaMe development adaptive scroll display
  • Next ASP basic knowledge VBScript basic elements explained