if (condition) { /* do something */ }
else { /* do something */ }
if (condition)
/* do something */
else
/* do something */
有人告诉我第一个实例不是个好主意。我不知道这是否真的是这种情况(或者第二个实例也是如此);这难道不能缩短输入的字符数吗?还是因为它只会产生混乱?
if (condition) { /* do something */ }
else { /* do something */ }
if (condition)
/* do something */
else
/* do something */
有人告诉我第一个实例不是个好主意。我不知道这是否真的是这种情况(或者第二个实例也是如此);这难道不能缩短输入的字符数吗?还是因为它只会产生混乱?
这两行内容不算是单独一行。
如果单行语句能让代码更易读,那么单行if
其实并没有什么问题。
比如像这样:
if (last_item) print ", and " else print ", "
比
更好。if (last_iem)
{
print ", and "
}
else
{
print ", "
}
我看过很多有愚蠢问题的第三方代码,因此我更喜欢始终使用大括号。话虽如此,我从未感觉良好。
if(){}
else (){}
当指令简短且单独时,我会在同一行使用if(){}。如果有else,则使用长格式:
if(checkSomething)
{
//dosomething
}
else
{
//doanotherthing
}
echo (last_item) ? ",and" : ", ";
。顺便尝试使用 echo。 - Bogdan Constantinescu