Vezérlőszerkezetek
Vezérlőszerkezetek alatt az if, for, while, switch stb. szerkezeteket
értjük. Példa az if utasításra (a legbonyolultabb közülük):
<?php
if ((feltetel1) || (feltetel2)) {
muvelet1;
} elseif ((feltetel3) && (feltetel4)) {
muvelet2;
} else {
defaultmuvelet;
}
?>
Vezérlőszerkezetek esetében hagyjunk egy szóközt a kulcsszó és a
nyitó zárójel között, hogy megkülönböztethessük őket a függvényhívásoktól.
Kapcsos zárójelek használata minden esetben erősen ajánlott, még
akkor is, amikor egyébként opcionálisak lennének. Alkalmazásuk növeli
az olvashatóságot, és csökkenti a logikai hibák előfordulásának
esélyét a kód új sorokkal történő bővítésénél.
Switch utasításoknál:
<?php
switch (feltetel) {
case 1:
muvelet1;
break;
case 2:
muvelet2;
break;
default:
defaultmuvelet;
break;
}
?>
Bekezdés és sorhossz (Previous)
|
(Next) Függvényhívások
|
|
|
Download Documentation
|
Last updated: Sun, 18 Oct 2009 |
|
Do you think that something on this page is wrong? Please file a bug report or add a note.
|
| User Notes: |
Note by: bobvandell@hotmail.com
I'm with Maga on this one. That's how I've been doing it for years.
Indent from the case so that the case it's self stands out.
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?
|
|