我碰到這個腳本來在http://www.php.net/manual/en/function.str-split.php#78040是`和`最佳實踐
/**
Returns a formatted string based on camel case.
e.g. "CamelCase" -> "Camel Case".
*/
function FormatCamelCase($string) {
$output = "";
foreach(str_split($string) as $char) {
strtoupper($char) == $char and $output and $output .= " ";
$output .= $char;
}
return $output;
}
的珍玩部分:
strtoupper($char) == $char and $output and $output .= " ";
我的問題
- 詳細突破
strtoupper($char) == $char and $output and $output .= " ";
下來,爲什麼它有效 - 這不適用於
break
,return
,echo
但它適用於print
- 任何功能,包括是否爲
if (strtoupper($char) == $char) { if ($output) { $output .= " "; } }
對於這個最佳實踐
- 做這樣的代碼有什麼優勢或劣勢
我不會在PHP中使用'和',雖然它會工作 - 使用'&&'而不是。 – halfer
@halfer:它不適用於&&,因爲'和'的優先級低於'。='和'&&' - 高。所以在技術上它會毫無錯誤地執行,但不會像預期的那樣運行。 – zerkms
謝謝@zerkms。我不知道'&&'和'和'有不同的優先級 - 值得了解! – halfer