Home Backend Development PHP Tutorial Things to note when using & (and operator) reference assignment in php foreach_PHP Tutorial

Things to note when using & (and operator) reference assignment in php foreach_PHP Tutorial

Jul 21, 2016 pm 03:40 PM
amp foreach php and use exist Quote Notice of want Assignment operator pass question

foreach can easily modify the elements of the array by adding & before $value, such as:

PHP code

Copy code The code is as follows :

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

But this usage is also easy to cause It's wrong and not easy to find.

It is more straightforward to look at the example:

PHP code
Copy the code The code is as follows:

$arr = array('a','b','c');
$arr2 = array('d', 'e', ​​'f');

foreach($arr as $value){//Used to use $value or $val
$value .= '4';
}

//After everything is processed, we will add it to the page Template output, first output $arr2
foreach($arr2 as $value){//Used to use $value or $val
//echo $value;
}
//Then output like this $arr;
foreach($arr as $value){//Used to use $value or $val
echo $value, "n";
}
?>

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

XML/HTML code
a4
b4
b4

The result is different from what I expected. This is a problem caused by quotation.

At the end of foreach($arr as &$value) array traversal, 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:

XML/HTML code
Copy code The code is as follows:

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

This is different from the final output 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 the key is 2, the value of $arr[2] becomes the value of $arr[2], 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.

PHP code
Copy code The code is as follows:

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/321277.htmlTechArticleforeach By adding } before $value, but this usage is also easy to cause errors, and it is not easy to find. It is more straightforward to look at the example: PHP code Copy the code The code is as follows: ?php $arr...
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

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

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

CakePHP Project Configuration CakePHP Project Configuration Sep 10, 2024 pm 05:25 PM

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

CakePHP Date and Time CakePHP Date and Time Sep 10, 2024 pm 05:27 PM

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP File upload CakePHP File upload Sep 10, 2024 pm 05:27 PM

To work on file upload we are going to use the form helper. Here, is an example for file upload.

CakePHP Routing CakePHP Routing Sep 10, 2024 pm 05:25 PM

In this chapter, we are going to learn the following topics related to routing ?

Discuss CakePHP Discuss CakePHP Sep 10, 2024 pm 05:28 PM

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP Creating Validators CakePHP Creating Validators Sep 10, 2024 pm 05:26 PM

Validator can be created by adding the following two lines in the controller.

See all articles