Home > Backend Development > PHP Tutorial > PHP coding standards (10)_PHP tutorial

PHP coding standards (10)_PHP tutorial

WBOY
Release: 2016-07-13 17:20:18
Original
873 people have browsed it

5.2 Initialization

Try to initialize local variables while declaring them. The only reason not to do this is if the initial value of the variable depends on some computation that occurred previously.

5.3 Layout

Declare variables only at the beginning of a code block. (A block is any code enclosed between curly braces "{" and "}".) Do not declare a variable the first time it is used. This can confuse programmers who are distracted, and can hinder the portability of code within that scope.


function myMethod() {
 int $int1 = 0; // Beginning of method block

 if ($condition) {
 int $int2 = 0; // Beginning of "if" block
 ...
 }
}

One exception to this rule is the index variable of a for loop


for (int $i = 0; i < $maxLoops; $i++) { ... }

Prevent declared local variables from overwriting variables declared at the previous level. For example, don't declare the same variable name inside an inner code block:


int $count;
...
function myMethod() {
 if ($condition) {
 int $count = 0; // Avoid this declaration
  ...
 }
 ...
}


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532594.htmlTechArticle5.2 Initialization Try to initialize local variables at the same time as declaring them. The only reason not to do this is if the initial value of the variable depends on some computation that occurred previously. 5.3 Layout Only at the beginning of the code block...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template