search
HomeBackend DevelopmentPHP TutorialProblem with foreach loop using & (and operator)
Problem with foreach loop using & (and operator)Jun 23, 2017 pm 03:05 PM
foreachusecycleoperatorquestion

foreach It is easy to modify the array unit by adding & before $value. Be careful when using reference in foreach. You can also disconnect the reference relationship immediately after processing, so that the above situation will not happen later.

foreach($arr as $value){ 
$value .= '4'; 
}

But this usage is also easy to cause errors, and it is not easy to find.
It’s more straightforward to look at the example:

<?php 
$arr = array(&#39;a&#39;,&#39;b&#39;,&#39;c&#39;); 
$arr2 = array(&#39;d&#39;, &#39;e&#39;, &#39;f&#39;); 

foreach($arr as $value){//习惯用$value或$val 
$value .= &#39;4&#39;; 
} 

//都处理完毕我们在页面模版输出,首先输出$arr2 
foreach($arr2 as $value){//习惯用$value或$val 
//echo $value; 
} 
//然后再这样输出 $arr; 
foreach($arr as $value){//习惯用$value或$val 
echo $value, "\n"; 
} 
?>

Let’s see if the output result is the same as expected. The result here is:

a4
b4
b4

The result is different from what I expected. This is the problem caused by the reference.
When foreach($arr as &$value) traverses the array to the end, the reference relationship is not broken. This is equivalent to the reference of the last unit of $value and $arr, which is $arr[2].
Going to foreach($arr2 as $value), the value of $value always changes with the value of the array unit. Since the reference relationship is not broken, the value of $arr[2] also changes accordingly. Until $arr2 is traversed, the value of $value is f, so the value of $arr[2] is also f.
At this time, the value of $arr should be:

Array 
( 
[0] => a4 
[1] => b4 
[2] => f 
)

This is different from the final output result we see. Then go to foreach($arr as $value). In the same way, the value of $arr[2] also changes with $value. When the key is 1, that is, $arr[1], $arr[2] The value also becomes the value of $arr[1], which is b4. Then when iterating to key 2, the value of $arr[2] becomes the value of $arr[2] again, which is b4. It is the result of output.
So be careful when using references in foreach. You can also disconnect the reference relationship immediately after processing, so that the above situation will not happen later.

foreach($arr as $value){ 
$value .= &#39;4&#39;; 
} 
unset($value);


The above is the detailed content of Problem with foreach loop using & (and operator). For more information, please follow other related articles on the PHP Chinese website!

Statement
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
Java ArrayList遍历时使用foreach和iterator删除元素的区别是什么?Java ArrayList遍历时使用foreach和iterator删除元素的区别是什么?Apr 27, 2023 pm 03:40 PM

一、Iterator和foreach的区别多态差别(foreach底层就是Iterator)Iterator是一个接口类型,他不关心集合或者数组的类型;for和foreach都需要先知道集合的类型,甚至是集合内元素的类型;1.为啥说foreach底层就是Iterator编写的代码:反编译代码:二、foreach与iterator时remove的区别先来看阿里java开发手册但1的时候不会报错,2的时候就会报错(java.util.ConcurrentModificationException)首

php如何判断foreach循环到第几个php如何判断foreach循环到第几个Jul 10, 2023 pm 02:18 PM

​php判断foreach循环到第几个的步骤:1、创建一个“$fruits”的数组;2、创建一个计数器变量“$counter”初始值为0;3、使用“foreach”循环遍历数组,并在循环体中增加计数器变量的值,再输出每个元素和它们的索引;4、在“foreach”循环体外输出计数器变量的值,以确认循环到了第几个元素。

c语言开根号运算符是什么c语言开根号运算符是什么Mar 06, 2023 pm 02:39 PM

在c语言中,没有开根号运算符,开根号使用的是内置函数“sqrt()”,使用语法“sqrt(数值x)”;例如“sqrt(4)”,就是对4进行平方根运算,结果为2。sqrt()是c语言内置的开根号运算函数,其运算结果是函数变量的算术平方根;该函数既不能运算负数值,也不能输出虚数结果。

Java中的%是什么意思Java中的%是什么意思Mar 06, 2023 pm 04:48 PM

在Java中,“%”是取余的意思,是一个二元算术运算符,可进行除法运算并获取余数,语法“操作数1 % 操作数2”。取余运算符“%”的操作数通常是正整数也可以是负数甚至是浮点数,如果负数参与此运算,则结果的正负取决于前面一个数是正数还是负数。

golang 报错:“invalid use of … operator” 如何解决?golang 报错:“invalid use of … operator” 如何解决?Jun 24, 2023 pm 05:54 PM

对于Golang开发者来说,“invaliduseof…operator”是一个常见的报错。这个报错通常会在使用变长参数函数时出现。它在编译时就会被检测出来,并指出哪些部分有问题。这篇文章将介绍如何解决这个报错。一、什么是变长参数函数变长参数函数也被称为可变参数函数,是Golang语言中的一种函数类型。使用变长参数函数可以像如下方式定义多个

php中“==”符号的含义是什么php中“==”符号的含义是什么Mar 14, 2023 pm 07:05 PM

在php中,“==”符号是一个比较运算符,可以比较两个操作数是否相等,语法“操作数1 == 操作数2”。“==”运算符会比较、并测试左边的变量(表达式或常量)是否与右边的变量(表达式或常量)具有相同的值;它只比较变量的值,而不是数据类型。如果两个值相同,则返回true值;如果两个值不相同,则返回false值。

php怎么判断两个数能否整除php怎么判断两个数能否整除Jan 10, 2023 pm 03:12 PM

在php中,可以使用“%”和“==”运算符来判断两个数能否整除;只需要使用“%”运算符将两个数相除获取余数,再使用“==”运算符判断获取的余数是否为0即可,语法“数1 % 数2 == 0”,如果为0则能整除,如果不为0则不能整除。

PHP返回一个键值翻转后的数组PHP返回一个键值翻转后的数组Mar 21, 2024 pm 02:10 PM

这篇文章将为大家详细讲解有关PHP返回一个键值翻转后的数组,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。PHP键值翻转数组键值翻转是一种对数组进行的操作,它将数组中的键和值进行交换,生成一个新的数组,其中原始键作为值,原始值作为键。实现方法在php中,可以通过以下方法对数组进行键值翻转:array_flip()函数:array_flip()函数专门用于键值翻转操作。它接收一个数组作为参数,并返回一个新的数组,其中键和值已交换。$original_array=[

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

Hot Tools

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!