Code Coding for PHP website

Source: Internet
Author: User
Tags array comparison constant numeric value php website strcmp strlen uppercase letter
Code Coding for PHP website

The name of a variable

A) All letters use lowercase

b The first letter is specified according to the variable value type

I. Integer i

Ii. floating-point number F

Iii. string S

Iv. Boolean value B

V. Array A

Vi. object o

Vii. Resources R

Viii. Mixed Type M

C Use ' _ ' as a dividing line for each word

For example:

$i _age_max = 10;

$f _price = 22.5;

$s _name = ' Harry ';

$b _flag = true;

$a _price = Array ();

$o _object = new Class ();

$r _file = fopen ();

$m _var = array_combine ($a _name, $a _flag);

Second, class naming

A the use of uppercase letters as a separate word, the other letters are in lowercase, that is, the hump format.

b The first letter of the first name uses uppercase

c) Do not use the underline ('_')

d) The interface interface is best to use the uppercase letter I and end with interface

For example:

Class Nameonetwo

Class Name

Interface Iexampleinterface ()

Third, the method name

A the use of uppercase letters as a separate word, and all other letters using lowercase

b The first letter of the name is capitalized, declared as "private" or "protected", using ' _ ' as the prefix

c) Do not use the underline ('_')

d) (rules consistent with class naming)

E The accessors of an object are always prefixed with "get" or "set", and when a design pattern such as a single state mode (singleton) or a factory pattern (factory) is used, the method's name should contain the name of the pattern, so that it is easy to identify the design pattern from the name.

For example:

Class nameonetwo{

Public Function DoIt () {};

Protect function _handleerror () {};

Private Function _sayhello () {};

}

Four, class attribute naming

A the attribute name prefix should be specified as a property value type (specific reference variable naming rule)

b prefix followed by a rule that is consistent with the class name

c The private attribute is prefixed with ' _ '

For example:

Class Nameonetwo {

Public Function Varabc () {};

Public Function ErrorNumber () {};

Public $iAge;

Private $_iage;

}

Name of the parameter in the method

A) reference class attribute naming

For example:

Class Nameonetwo {

Public Function Varabc ($SMSG) {};

}

Six, global variables

A the global variable should be prefixed with ' g '

b The rest of the reference variable naming rules

For example:

Global $gi _age;

Global $ga _price

Vii. defining named/Global constants

A global constant uses ' _ ' to split each word

b) all letters using uppercase

For example:

Define (' E_error_missing_para ', 501);

Eight, function

A) All letters in lowercase, using ' _ ' to separate each word

For example:

function Some_bloody_function () {

}

Nine, curly braces {} rules

(a) Place the curly braces at the same column or peer below the keyword, recommended to the peer

For example:

function Some_bloody_function () {

}

if (true) {

Code

}

Ten, Indent/tab rules

A) using tab indents

For example:

function Some_bloody_function () {

if (true) {

Code

}

}

Xi. parentheses, keywords and function rules

A) do not put parentheses and keywords close together, to separate them with a space

b parentheses and function names are important to stick together

c) Do not use parentheses in return statements unless necessary

For example:

if (condition) {

}

strcmp ($sHello, $sEvening);

return 1;

12. If Then Else format

Layout

if (condition 1) {//Note

}

else if (condition 2) {//comment

}

else {//comment

}

If you use the else if statement, it is usually better to have an else block to handle other cases that are not handled. If you can put a record information note at else, even if there is no action.

Conditional formatting

Always place a constant on the left side of the equal/no equal sign, for example:

if (6 = $errorNum) ...

One reason is that if you omit an equal sign in the equation, the grammar checker will give you an error. The second reason is that you can find the value at once, rather than finding it at the end of your expression. It takes a little time to get used to the format, but it's really useful.

13. Switch format

A The default should always exist, it should not be reached, however, if it arrives it will trigger an error.

b If you want to create a variable, put all the code in the block.

For example:

Switch ($numPeople) {
Case 1:
Break

Case 2:
Break

Default
Break
}

14,? : Ternary operator

(a) Place the condition in parentheses to separate it from other code.

b if possible, the action can be in a simple function.

C Put the action, "?", ":" On different lines, unless they can be clearly placed on the same line.

For example

(condition)? Funct1 (): Func2 ();

(condition)

? Long statement

: another long statement;

The declaration of the Code block

A declares that the code block needs to be aligned.

For example

$iDate = 0;

$irDate = NULL;

$sName = ';

$mName = NULL;

16, do not use the default method to test non-0 values

Do not test a non-0 value with the default value, that is, use:

if (FAIL!= f ())

Better than the following methods:

if (f ())

Even if fail can contain a value of 0, that is, the representation of PHP as false. When someone decides to use 1 instead of 0 as a failure return value, an explicit test can help you. The comparison value does not change and should use explicit comparison;

For example: if (!) ( $bufsize% strlen ($STR))) should be written as: if ($bufsize% strlen ($str) = = 0) to indicate the numeric value (not Boolean) of the test. A frequent problem is using strcmp to test a character equation, and the result will never be equal to the default value.

Non-0 tests take the approach based on default values, and other functions or expressions are subject to the following restrictions:

Only 0 can be returned to indicate failure and cannot be/have other values.

Name so that a true (true) return value is absolutely obvious, calling the function IsValid () instead of the Checkvalid ().

17, PHP annotation and method analysis

A)//single note

b) the second type
/*
* Multi-line Comment ...
*
*/
c) The third type
/**
* Multi-line document annotation ...
*
*/

d Description of documentation comments

/**
* This is a comment about the function ...
*
*/
function Bac () {}

/*
* This is not a document comment, because it is not started with/** ...
*
*/
function Bac () {}

/**
* This is a document annotation, but it has nothing to do with the function, because he didn't put it in front of the function ...
*
*/

function Bac () {}

E) Document Description
/**
* File Description
* @author Author
* @license License
* @version 1.0
* @link
* @final
*/

It is recommended to use Zendstudio to reformat the code once the code is written and clear



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.