最佳做法是编写其他人可以轻松读取和更新的代码。
你的第一个表单是有问题的,因为它不遵循大多数PHP开发人员习惯的表单:
if (condition) {
// code
} else {
// code
}
// ... or ...
if (condition)
{
// code
}
else
{
// code
}
// ... or ...
if (condition) { /* short code */ } else { /* short code */ }
// ... or ...
condition ? /* short code */ : /* short code */;
请注意,这完全是关于标准实践的,并不一定有意义 - 它只是关于其他开发人员习惯于看到的内容。
更重要的是,你的第二种形式并不那么好,因为它使另一个程序员很容易犯这个错误:
if (condition)
// code A
else
// code B
// code C (added by another programmer)
在这个例子中,另一个程序员添加了,但忘记用大括号包装整个块。这将导致问题。您可以通过简单地将和块包裹在大括号中来防御这种情况。code C
else
if
else