PHP code specification (10) _ PHP Tutorial

Source: Internet
Author: User
PHP code specification (10 ). 5.2 perform initialization when declaring local variables. The only reason for not doing so is that the initial value of the variable depends on some previous calculations. 5.3 The layout is only available in the opening of the code block 5.2 initialization

Try to initialize the local variable while declaring it. The only reason for not doing so is that the initial value of the variable depends on some previous calculations.

5.3 layout

Declare variables only at the beginning of the code block. (A block refers to any code contained in braces "{" and .) Do not declare the variable when it is used for the first time. This will confuse unfocused programmers and impede code portability in this scope.


Function myMethod (){
Int $ int1 = 0; // start of the method block

If ($ condition ){
Int $ int2 = 0; // start of the "if" block
...
}
}

An exception to this rule is the index variable of the for loop.


For (int $ I = 0; I <$ maxLoops; $ I ++ ){...}

Avoid declaring local variables to overwrite the variables declared at the upper level. For example, do not declare the same variable name in the internal code block:


Int $ count;
...
Function myMethod (){
If ($ condition ){
Int $ count = 0; // avoid this declaration
...
}
...
}


Http://www.bkjia.com/PHPjc/532594.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/532594.htmlTechArticle5.2 initialization tries to initialize while declaring local variables. The only reason for not doing so is that the initial value of the variable depends on some previous calculations. 5.3 layout is only available in the code block...

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.