我正在阅读Pro PHP Programming并且在示例中作者在返回值周围使用括号
这有什么区别:
function foo($x) {
return (bar::baz($x));
}
还有这个:
function foo($x) {
return bar::baz($x);
}
?
我正在阅读Pro PHP Programming并且在示例中作者在返回值周围使用括号
这有什么区别:
function foo($x) {
return (bar::baz($x));
}
还有这个:
function foo($x) {
return bar::baz($x);
}
?
正如其他人所提到的,它们在功能上是相同的......几乎。如文档中所述:
注意:当通过引用返回时,你不应该在你的返回变量周围使用括号,因为这不起作用。您只能通过引用返回变量,而不是语句的结果。如果你使用 return ($a); 那么你返回的不是变量,而是表达式 ($a) 的结果(当然是 $a 的值)。
PHP 手册还指出了避免括号的(小)性能优势编辑...请参阅下面的基准编辑:
注意:请注意,由于 return 是一种语言结构而不是函数,因此不需要在其参数周围加上括号。将它们排除在外是很常见的,实际上您应该这样做,因为在这种情况下 PHP 要做的工作较少。
为了安全起见,可能值得避免使用括号。
我决定对 php.net 的注释进行测试,看看 PHP 在没有括号中的返回值的情况下需要做多少“更少的工作”。答案:即使按照微优化标准,也不值得担心。我将此测试(v5.3)设置为通过两个相同的函数循环 100,000 次:
function test1(){
//do something
$a=1;
return $a;
}
function test2(){
//do something
$a=1;
return ($a);
}
$array = array();
for($j=0;$j<100;$j++){
$array[$j] = array();
$time = microtime(true);
$val = 0; //set a dummy variable
for($i=0;$i<100000;$i++){
$val = test1();
}
$array[$j][0] = microtime(true)-$time;
unset($i);
unset($val);
unset($time);
$time = microtime(true);
$val = 0; //set a dummy variable
for($i=0;$i<100000;$i++){
$val = test2();
}
$array[$j][1] = microtime(true)-$time;
unset($i);
unset($val);
unset($time);
}
$without_p = 0;
$with_p = 0;
foreach($array as $values){
$without_p +=$values[0];
$with_p +=$values[1];
echo $values[0].' vs '.$values[1]."\n";
}
echo "---------------\nAverages: \n".($without_p/count($array))." vs ".($with_p/count($array));
结果如下:
0.032001972198486 vs 0.028002023696899
0.032001972198486 vs 0.028001070022583
0.032001972198486 vs 0.028002023696899
0.028001070022583 vs 0.032001972198486
0.028002023696899 vs 0.03200101852417
0.02800178527832 vs 0.028002023696899
0.032001972198486 vs 0.028001070022583
0.032001972198486 vs 0.028002023696899
0.028001070022583 vs 0.032001972198486
0.028002023696899 vs 0.03200101852417
0.028002023696899 vs 0.028002023696899
0.03200101852417 vs 0.02800178527832
0.032002210617065 vs 0.028000831604004
0.028002023696899 vs 0.032001972198486
0.028001070022583 vs 0.028002023696899
0.032001972198486 vs 0.028001070022583
0.032001972198486 vs 0.028002023696899
0.028000831604004 vs 0.032002210617065
0.02800178527832 vs 0.028001070022583
0.032001972198486 vs 0.028002023696899
0.032001972198486 vs 0.028001070022583
0.028002023696899 vs 0.032001972198486
0.028001070022583 vs 0.032001972198486
0.028002023696899 vs 0.028000831604004
0.032002210617065 vs 0.028002023696899
0.032001972198486 vs 0.028000831604004
0.028002023696899 vs 0.032001972198486
0.028001070022583 vs 0.032001972198486
0.028002023696899 vs 0.028001070022583
0.032001972198486 vs 0.028002023696899
0.028000831604004 vs 0.032002210617065
0.028002023696899 vs 0.032000780105591
0.028002023696899 vs 0.032001972198486
0.028001070022583 vs 0.032001972198486
0.028002023696899 vs 0.028001070022583
0.032001972198486 vs 0.028002023696899
0.032001972198486 vs 0.028001070022583
0.028002023696899 vs 0.032001972198486
0.032001972198486 vs 0.03200101852417
0.032001972198486 vs 0.032001972198486
0.028002023696899 vs 0.03200101852417
0.032001972198486 vs 0.032001972198486
0.028002023696899 vs 0.028001070022583
0.032001972198486 vs 0.028002023696899
0.032001972198486 vs 0.028001070022583
0.02800178527832 vs 0.032002210617065
0.028000831604004 vs 0.028002023696899
0.032001972198486 vs 0.028001070022583
0.032001972198486 vs 0.028002023696899
0.03200101852417 vs 0.028002023696899
0.032001972198486 vs 0.028001070022583
0.028002023696899 vs 0.032001972198486
0.028000831604004 vs 0.028002023696899
0.032001972198486 vs 0.028001070022583
0.032001972198486 vs 0.028002023696899
0.032001972198486 vs 0.028001070022583
0.028002023696899 vs 0.032001972198486
0.028001070022583 vs 0.032001972198486
0.028002023696899 vs 0.028000831604004
0.032001972198486 vs 0.028002023696899
0.032001972198486 vs 0.028001070022583
0.032001972198486 vs 0.028002023696899
0.03200101852417 vs 0.028002023696899
0.028002023696899 vs 0.03200101852417
0.032001972198486 vs 0.028002023696899
0.032001972198486 vs 0.028001070022583
0.028002023696899 vs 0.032001972198486
0.028000831604004 vs 0.028002023696899
0.032001972198486 vs 0.028001070022583
0.032001972198486 vs 0.028002023696899
0.032001972198486 vs 0.028001070022583
0.032001972198486 vs 0.032001972198486
0.028001070022583 vs 0.028002023696899
0.032001972198486 vs 0.032001972198486
0.028001070022583 vs 0.028002023696899
0.032001972198486 vs 0.028000831604004
0.032002210617065 vs 0.028002023696899
0.032000780105591 vs 0.028002023696899
0.028002023696899 vs 0.03200101852417
0.028002023696899 vs 0.028002023696899
0.03200101852417 vs 0.028002023696899
0.032001972198486 vs 0.028002023696899
0.028001070022583 vs 0.032001972198486
0.028002023696899 vs 0.032000780105591
0.028002023696899 vs 0.028002023696899
0.03200101852417 vs 0.028002023696899
0.028002023696899 vs 0.03200101852417
0.028002023696899 vs 0.032001972198486
0.028001070022583 vs 0.028002023696899
0.032001972198486 vs 0.028000831604004
0.028002023696899 vs 0.032001972198486
0.028001070022583 vs 0.032001972198486
0.028002023696899 vs 0.028001070022583
0.032001972198486 vs 0.032001972198486
0.032001972198486 vs 0.032001972198486
0.032001972198486 vs 0.03200101852417
0.032001972198486 vs 0.028002023696899
0.032001972198486 vs 0.028001070022583
0.028002023696899 vs 0.032001972198486
0.028001070022583 vs 0.028002023696899
--------------- Averages:
0.030041754245758 vs 0.029521646499634
因此(通过运行此测试 100 次消除外部影响后)100,000 次循环后的总影响几乎为 0.0005 秒。这里的基本结论是,这里的性能损失(至少在处理时间方面)非常小......甚至在 100,000 次循环之后,版本括号有时比使用推荐的裸返回更快。
当然,这仍然值得避免,因为您最终会尝试通过引用返回,并且会花费数小时试图找到错误的根源,但性能确实不是一个有效的论据。
似乎只是作者使用的编码风格约定。他大概认为它更清晰,更容易阅读,也更容易让新手理解。但它实际上对返回的值没有任何影响。