Darunter fallen if, for, while, switch usw.
Hier ist ein Beispiel für eine if-Kosntruktion, da sie eine
der komplizierten ist:
<?php
if ((condition1) || (condition2)) {
action1;
} elseif ((condition3) && (condition4)) {
action2;
} else {
defaultaction;
}
?>
|
Kontroll-Ausdrücke sollten ein Leerzeichen zwischen den Schlüsselwörtern
und der öffnenden Klammer haben, um sie von Funktionsaufrufen unterscheiden zu können.
Sie sollten unbedingt geschweifte Klammern verwenden, auch wenn
sie technisch nur optional sind. Damit verbessern Sie die Lesbarkeit
und vermeiden logische Fehler, wenn neue Zeilen hinzugefügt werden.
Für switch-Ausdrücke:
<?php
switch (condition) {
case 1:
action1;
break;
case 2:
action2;
break;
default:
defaultaction;
break;
}
?>
|
|
Coding Standards (Previous)
|
(Next) Funktionsaufrufe
|
|
|
Download Documentation
|
Last updated: Sun, 28 Sep 2008 |
|
Do you think that something on this page is wrong? Please file a bug report or add a note.
|
| User Notes: |
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?
|
|