


A pitfall of PHP loops and references, PHP loop references
on the code
<span>$arr</span> = <span>array</span><span>( </span>'a'=> 'a11', 'b'=> 'b22', 'c'=> 'c33',<span> ); </span><span>foreach</span> (<span>$arr</span> <span>as</span> <span>$k</span>=>&<span>$v</span><span>){ </span><span>//</span><span> Do somethind</span> <span>} </span><span>foreach</span> (<span>$arr</span> <span>as</span> <span>$k</span>=><span>$v</span><span>){ </span><span>var_dump</span>(<span>$v</span><span>); }</span>
With such code, what will var_dump output? You can try it, the answer is
<span>string</span>(3) "a11" <span>string</span>(3) "b22" <span>string</span>(3) "b22"
In the result, the third line becomes the value of key='b'. The problem lies in the first circular reference.
I stepped into such a pit today, and it took me a long time to check the problem. Simply put, the references used in the foreach loop are retained after the loop ends. php.net rewrote:
Warning
Reference of a $value and the last array element remain even after the foreach loop. It is recommended to destroy it by unset().
For the above example, after the first loop ends, the $v reference still exists. Since the variables of the two loops are named the same, $v will be assigned a value every time when the second loop starts. Until the end, $v is Set to
The value of the previous element.
The principle is very simple, and the documentation is clearly written. But if you encounter related bugs at work, it will be very difficult, and it will take a long time to locate. When you still need to write code, pay attention to:
1. Reduce the use of references
2. If you need to use a reference in foreah, you should encapsulate it with a function

我们知道在C语言中,'while'关键字用于定义一个循环,该循环根据传递给循环的条件来工作。现在,由于条件可以有两个值,即真或假,所以如果条件为真,则while块内的代码将被重复执行,如果条件为假,则代码将不会被执行。现在,通过将参数传递给while循环,我们可以区分while(1)和while(0),因为while(1)是一个条件始终被视为真的循环,因此块内的代码将开始重复执行。此外,我们可以说明,传递给循环并使条件为真的不是1,而是如果任何非零整数传递给while循环,则它将被视为真条件,因

while的用法是“while 条件: 代码块”,条件是一个表达式,当条件为真时,执行代码块,然后再次判断条件是否为真,如果为真则继续执行代码块,直到条件为假为止。while是一个常用的循环控制语句,用于在满足一定条件的情况下重复执行一段代码块。

在go语言中,while不是关键字,可以用for语句加break来实现while循环的效果,例“for {sum++ if sum>10{break}else{...}}”。go语言有break、default 、func、select、case、defer、go、map、else、goto、for、if、var等25个关键字。

Python中常见的流程控制结构有哪几种?在Python中,流程控制结构是用来决定程序的执行顺序的重要工具。它们允许我们根据不同的条件执行不同的代码块,或者重复执行一段代码。下面将介绍Python中常见的流程控制结构,并提供相应的代码示例。条件语句(if-else):条件语句允许我们根据不同的条件执行不同的代码块。它的基本语法是:if条件1:#当条件

PHP中break关键字的作用和用法详解在PHP编程中,break是一种控制流语句,用于中断当前循环或switch语句,并跳出该循环或switch。本文将详细介绍break关键字的作用和用法。一、循环中的break在循环结构中,break的作用是提前终止循环,跳出循环体执行循环后的代码。常见的循环结构有for、while和do...while。在for循环中

说明1、while关键词的中文含义是当……时,即条件成立时循环执行对应的代码。while语句是循环语句中的基本结构,语法格式比较简单。执行过程2、执行while语句时,首先判断循环条件,循环条件为false时,直接执行while语句的后续代码,循环条件为true时,执行循环体代码,判断循环条件,直到循环条件不成立为止。实例inti=1;intsum=0;while(i

Java代码publicvoidhandleConnection(SocketconnectionToHandle){newThread(newConnectionHandler(connectionToHandle)).start();}publicvoidhandleConnection(SocketconnectionToHandle){newThread(newConnectionHandler(connectionToHandle)).start();}我们对RemoteFileSer

循环与迭代:概念解析循环是一种控制结构,它允许代码块重复执行指定的次数或直到满足特定条件。python提供了多种循环类型,包括for循环、while循环和do-while循环。另一方面,迭代是一个抽象概念,它表示按顺序遍历序列元素的过程。Python提供了迭代器和生成器等工具来实现迭代。循环vs.迭代:异同点执行机制:循环显式控制执行流程,而迭代是通过迭代器对象隐式执行的。状态管理:循环保持其自己的状态(如计数器或条件),而迭代器封装了状态管理。使用场景:循环适用于需要重复执行固定次数或直到满足


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 English version
Recommended: Win version, supports code prompts!

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.

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools