Home  >  Q&A  >  body text

参数前加&是什么意思?引用传值?

这是一段简单的测试代码执行时间的程序,只是不明白为什么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);


phpcn_u699phpcn_u6992699 days ago2038

reply all(2)I'll reply

  • 数据分析师

    数据分析师2017-10-01 00:30:04

    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.

    reply
    0
  • 巴扎黑

    巴扎黑2017-02-24 09:51:39

    引用传值,函数内的参数与传入参数执行同一个变量的引用,函数内对变量的修改在函数结束之后依然有效。如果不是引用传递,函数内操作的是一个局部变量,函数退出之后,不影响外部变量的值

    reply
    1
  • Cancelreply