Home > php教程 > PHP源码 > foreach遍历数组一个容易忽略的小错误,你曾经犯过没?

foreach遍历数组一个容易忽略的小错误,你曾经犯过没?

PHP中文网
Release: 2016-05-25 17:13:32
Original
983 people have browsed it

foreach遍历数组一个容易忽略的小错误,你曾经犯过没?

<?php
/**
 * 注意foreach取值的差异
 */
$arr = array(1,2,3,4);

echo "foreach遍历数组1:
";
foreach($arr as $val) {
    $val = $val*2;
}

var_dump($arr);

echo "
foreach遍历数组2:
";
foreach($arr as &$val) {
    $val = $val*2;
}

var_dump($arr);
/*end of php*/
Copy after login

                   

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template