Hidden Danger of References inside a Foreach Construct

In order to be able to directly modify array elements within the loop precede $value with &. In that case the value will be assigned by reference.

That's a fact we've known for years. But I don't use it very often for it never works for me. My code is simplified as follows.

<?php

//This is the file content
$file = '<?php exit;?>
::1	1365393847	/healthcom/memcp.do	{"idnum":"450521199309333489","password":"123459"}	{"action":"login"}
::1	1365393858	/healthcom/register.do	{"info":"thisis a test"}	[]
::1	1365393869	/healthcom/memcp.do	{"idnum":"450521199309333489","password":"123459"}	{"action":"login"}';

//We read it and explode it into an array
$logs = explode("\r\n", $file);
unset($logs[0]);

//Handle each line
foreach($logs as &$log){
	if(empty($log)){
		continue;
	}

	$log = explode("\t", $log);
	$log[1] = date($log[1]);
}

//Output the content directly. (Maybe you'd rather output the HTML in another file with a template engine.)
foreach($logs as $log){
	print_r($log);
}

?>

There seems no problems. Unfortunately I got the output like this:

Array
(
    [0] => ::1
    [1] => 1365393847
    [2] => /healthcom/memcp.do
    [3] => {"idnum":"450521199309333489","password":"123459"}
    [4] => {"action":"login"}
)
Array
(
    [0] => ::1
    [1] => 1365393858
    [2] => /healthcom/register.do
    [3] => {"info":"thisis a test"}
    [4] => []
)
Array
(
    [0] => ::1
    [1] => 1365393858
    [2] => /healthcom/register.do
    [3] => {"info":"thisis a test"}
    [4] => []
)

The last record was unexpectedly modified. I've been trying to find out what the problem it is. 


There's a warning in the PHP manual.

Reference of a $value and the last array element remain even after theforeach loop. It is recommended to destroy it by unset().


In the foreach construct, we've seen the $log is assigned a new value for each line. But the previous $log won't be affected because PHP seems to unset the reference on each loop ends and then start the next loop. But what on hell happened? Why the final log was modified in the second foreach construct?

I tried to output the $logs with print_r($logs); instead of a foreach. It wasn't modified.

It's noticed that reference $log wasn't destroyed in the last loop of the first statement. So when the second one starts, $log (references to $logs[3]) was assigned a new value, $logs[1].


In conclusion, although $log won't be affected in each loop, $log will be still there until you unset it. And as an easily overlooked assignment statement, the foreach construct can be a hidden danger. Just unset every reference when it becomes unnecessary.


<?php

foreach($logs as &$log){
//Handle your $log
}
unset($log); //Don't miss me after each foreach construct with a reference. I'm in the PHP manual, but easily overlooked.

?>

Ha, now it works as expected~

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值