我在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在内的任何函数。发布于 2012-10-06 10:38:58
这和
if (strtoupper( $char ) == $char) {
if ($output) {
$output .= " ";
}
}对于代码A and B,如果A被求值为true,则将执行A。
&&与and的区别是&&优先于and,.=优先于and。
发布于 2012-10-06 10:49:49
评估
正如其他答案所指出的,每个后续语句只在前面的statement==true被执行时才会执行。
这在代码中更相关,比如: if(foo和bar) { //do }
如果是foo==false,那么就没有必要浪费时间评估栏了。
我不能说我在布尔逻辑之外的优势是使用短路评估,而且为了让其他程序员看我的代码,我可能现在就不会开始了。
发布于 2012-10-06 10:41:24
strtoupper( $char ) == $char and $output and $output .= " ";是一种短手,如果它首先检查它是否是大写字符,如果是,他将转到下一个,检查$output是否为空,然后向$output添加一个空格。
这不是最佳实践,但使用一行感觉很酷。
好处是它很酷,缺点是你需要反复阅读才能理解它。
https://stackoverflow.com/questions/12759011
复制相似问题