{10593} - Control Structures
Control structures are for example "if", "for", "foreach", "while", "switch" etc. Below, an example with "if":
<?php
if ((expr_1) || (expr_2)) {
// action_1;
} elseif (!(expr_3) && (expr_4)) {
// action_2;
} else {
// default_action;
}
?>
<?phpif ((expr_1) || (expr_2)) {// action_1;} elseif (!(expr_3) && (expr_4)) {// action_2;} else {// default_action;}?>
- In the control structures there should be 1 (one) space before the first parenthisis and 1 (one) space between the last parenthisis and the opening bracket.
- Always use curly brackets in control structures, even if they are not needed. They increase the readability of the code, and they give you fewer logical errors.
- Opening curly brackets should be placed on the same line as the control structure. Closing curly brackets should be placed on new lines, and they should have same indentation level as the control structure. The statement included in curly brackets should begin on a new line, and code contained within it should gain a new level of indentation.
<?php
// wrong - no brackets, badly placed statement
if (expr) statement;
// wrong - no brackets
if (expr)
statement;
// good
if (expr) {
statement;
}
?>
<?php// wrong - no brackets, badly placed statementif (expr) statement;// wrong - no bracketsif (expr)statement;// goodif (expr) {statement;}?>
{11564} - Control Structures
Control structures are for example "if", "for", "foreach", "while", "switch" etc. Below, an example with "if":
<?php
if ((expr_1) || (expr_2)) {
// action_1;
} elseif (!(expr_3) && (expr_4)) {
// action_2;
} else {
// default_action;
}
?>
<?phpif ((expr_1) || (expr_2)) {// action_1;} elseif (!(expr_3) && (expr_4)) {// action_2;} else {// default_action;}?>
- In the control structures there should be 1 (one) space before the first parenthesis and 1 (one) space between the last parenthesis and the opening bracket.
- Always use curly brackets in control structures, even if they are not needed. They increase the readability of the code, and they give you fewer logical errors.
- Opening curly brackets should be placed on the same line as the control structure. Closing curly brackets should be placed on new lines, and they should have same indentation level as the control structure. The statement included in curly brackets should begin on a new line, and code contained within it should gain a new level of indentation.
<?php
// wrong - no brackets, badly placed statement
if (expr) statement;
// wrong - no brackets
if (expr)
statement;
// good
if (expr) {
statement;
}
?>
<?php// wrong - no brackets, badly placed statementif (expr) statement;// wrong - no bracketsif (expr)statement;// goodif (expr) {statement;}?>
Differences
| Lines: 11-19 | Lines: 11-19 | ||
| } | } | ||
| ?> | ?> | ||
| </pre> | </pre> | ||
| <ul> | <ul> | ||
| - | <li>In the control structures there should be 1 (one) space before the first parenthesis and 1 (one) space between the last parenthesis and the opening bracket.</li> | + | <li>In the control structures there should be 1 (one) space before the first parenthisis and 1 (one) space between the last parenthisis and the opening bracket.</li> |
| <li>Always use curly brackets in control structures, even if they are not needed. They increase the readability of the code, and they give you fewer logical errors.</li> | <li>Always use curly brackets in control structures, even if they are not needed. They increase the readability of the code, and they give you fewer logical errors.</li> | ||
| <li>Opening curly brackets should be placed on the same line as the control structure. Closing curly brackets should be placed on new lines, and they should have same indentation level as the control structure. The statement included in curly brackets should begin on a new line, and code contained within it should gain a new level of indentation.</li> | <li>Opening curly brackets should be placed on the same line as the control structure. Closing curly brackets should be placed on new lines, and they should have same indentation level as the control structure. The statement included in curly brackets should begin on a new line, and code contained within it should gain a new level of indentation.</li> | ||
| </ul> | </ul> | ||
| <pre> | <pre> | ||


























