Home  >  Article  >  Backend Development  >  PHP coding standards (5)_PHP tutorial

PHP coding standards (5)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 17:20:28830browse

3.6 Line break

When an expression cannot fit on one line, it can be broken according to the following general rules:

- Break after a comma
- Break before an operator
- Prefer a higher-level break to a lower-level one Break
- A new line should be aligned with the beginning of an expression of the same level as the previous line
- If the above rule clutters your code or makes your code cluttered to the right, use Break instead Enter 8 spaces.

Here are some examples of disconnected method calls:


someMethod(longExpression1, longExpression2, longExpression3,
   longExpression4, longExpression5);

$var = someMethod1(longExpression1,
  someMethod2(longExpression2,
      longExpression3));

The following are two examples of disconnected arithmetic expressions. The former is better because the break is outside the bracket expression, which is a higher-level break.


$longName1 = $longName2 * ($longName3 + $longName4 - $longName5)
      + 4 * $longname6; //Use this indentation method

$longName1 = $longName2 * ($longName3 + $longName4
     - $longName5) + 4 * $longname6; //Avoid this

The following are two examples of indented method declarations. The former is the norm. If the latter uses the regular indentation method, the second and third lines will be moved very far to the right, so instead they are indented by 8 spaces


//Traditional indentation method
function someMethod($anArg, $anotherArg, $yetAnotherArg,
     $andStillAnother) {
...
}

//Use 8 consecutive spaces to avoid transitional indentation
function horkingLongMethodName($anArg,
  $anotherArg, $yetAnotherArg,
  $andStillAnother) {
...
}

The line wrapping of if statements usually uses the 8-space rule, because regular indentation (4 spaces) will make the statement body look more laborious. For example:


//Don’t use this indentation method
if ((condition1 && condition2)
 || (condition3 && condition4)
 ||!(condition5 && condition6)) { // Wrong line break, no indentation
doSomethingAboutIt(); //The condition is aligned with this sentence, which makes it possible to miss this sentence when reading the program
}

//This indentation method should be used
if ((condition1 && condition2)
  || (condition3 && condition4)
  ||!(condition5 && condition6)) {
doSomethingAboutIt( );
}

//Or this indentation method can also be used
if ((condition1 && condition2) || (condition3 && condition4)
  ||!(condition5 && condition6)) {
 doSomethingAboutIt();
}

Here are three possible ways to handle ternary expressions:

$alpha = (aLongBooleanExpression) ? beta : gamma;

$alpha = (aLongBooleanExpression) ? beta
       : gamma;

$alpha = (aLongBooleanExpression)
 ? beta
  : gamma;


www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/532599.htmlTechArticle3.6 Line breaks When an expression cannot fit on one line, it can be broken according to the following general rules: - In Break after a comma - Break before an operator - prefer...
Statement:
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