这是一段简单的测试代码执行时间的程序,只是不明白为什么begin方法中的start变量前加&
<?php function begin(&$start) { $tmp = gettimeofday(); $start = $tmp[usec];} function report($start) { $tmp = gettimeofday(); $now = $tmp[usec]; echo "time consuming: "; echo $now - $start; echo "<br>";} function getRandNum($length = 8) { $salt = substr(uniqid(rand()), -$length); return $salt;} $start = 0;begin($start); $str=""; while (strlen($str)< 10000){ $str.=getRandNum(1);}report($start);
What does adding & before the parameter mean? Pass by value? -PHP Chinese website Q&A-What does adding & before a parameter mean? Pass by value? -PHP Chinese website Q&A
Let’s take a look and learn.
引用传值,函数内的参数与传入参数执行同一个变量的引用,函数内对变量的修改在函数结束之后依然有效。如果不是引用传递,函数内操作的是一个局部变量,函数退出之后,不影响外部变量的值