Php: what's the difference between $var and &$var?

前端 未结 5 1816
心在旅途
心在旅途 2021-02-18 22:36

What is the difference between

foreach ($my_array as $my_value) {
}

And:

foreach ($my_array as &$my_value) {
}
5条回答
  •  长发绾君心
    2021-02-18 22:49

    I only use references when i have to read csv files to know what is the delimitier. Just take the first line:

    $handle = fopen($file, "r");
    $firstLine = fgets($handle);
    fclose($handle);
    

    Then with this possible delimitiers

    $delimiters = array(';' => 0,',' => 0,"\t" => 0,"|" => 0);
    

    Count by reference what is the most used

    foreach ($delimiters as $delimiter => &$count) {
        $count = count(str_getcsv($firstLine, $delimiter));
    }
    $delimiter =  array_search(max($delimiters), $delimiters);
    

提交回复
热议问题