制御構造には、if, for, while, switch 等があります。
最も複雑な制御構造である if 文の例を示します。
<?php
if ((condition1) || (condition2)) {
action1;
} elseif ((condition3) && (condition4)) {
action2;
} else {
defaultaction;
}
?>
|
制御構造では、関数コールと区別するために、
制御キーワードと開きカッコの間に空白を 1 つ置きます。
構文的に省略可能な場合でも、波カッコを使用することを推奨します。
波カッコを付けることにより可読性が向上し、
新しく行を追加した際に論理的なエラーが紛れこむ可能性が減少します。
switch 文の場合は次のようにします。
<?php
switch (condition) {
case 1:
action1;
break;
case 2:
action2;
break;
default:
defaultaction;
break;
}
?>
|
|
標準コーディング規約 (Previous)
|
(Next) 関数コール
|
|
|
Download Documentation
|
Last updated: Sun, 05 Oct 2008 |
|
Do you think that something on this page is wrong? Please file a bug report or add a note.
|
| User Notes: |
My strong belief is still that this would add a nice way to represent loops...
switch (condition)
{
case 1;
action 1;
break1;
case 2;
action 2;
break2;
}
This makes the system much more readable and easy to understand. Every indent specifies a child. and two indents is especially useful when you are reading about 100 lines of code. It truely helps!
Note by: Maga
I think that better is:
<?php
switch (condition)
{
case 1:
action1;
break;
case 2:
action2;
break;
default:
defaultaction;
break;
}
Indentation+Brakes
?>
Phil, the indentation like in your example (i.e. with 4 spaces for the "case" statements) is also accepted in the PEAR coding standards.
Note by: phil@signalz.com
I would have expected more indentation, like this
<?php
switch (condition) {
case 1:
action1;
break;
case 2:
action2;
break;
default:
defaultaction;
break;
}
?>
Note by: dpn12@comcast.net
Might you consider adding to your current K&R format, below
...
switch (condition) {
case 1:
action1;
break;
case 2:
action2;
break;
}
...
/**/
...
switch (condition)
{
case 1:
action1;
break;
case 2:
action2;
break;
}
...
this above format that some believe to be more readable?
|
|