search
Homephp教程php手册php循环语句for和foreach的区别

php循环语句for和foreach的区别

Jun 06, 2016 pm 07:50 PM
forforeachphpthe differencecyclestatementEnter

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入 php中循环语句for和foreach的区别: php从版本4开始引入了 foreach 结构,这和perl以及其他语言很类似.这只是一种遍历数组的简便方法.foreach 仅能用于数组,当试图将其用于其它数据类型或者一个未初始

欢迎进入Linux社区论坛,与200万技术人员互动交流 >>进入

    php中循环语句for和foreach的区别:

    php从版本4开始引入了 foreach 结构,这和perl以及其他语言很类似.这只是一种遍历数组的简便方法.foreach 仅能用于数组,当试图将其用于其它数据类型或者一个未初始化的变量时会产生错误.

    有两种语法,第二种比较次要但却是第一种的有用的扩展.

    foreach (array_expression as $value){

    ... ...

    }

    foreach (array_expression as $key => $value){

    ... ...

    }

    第一种格式遍历给定的 array_expression 数组.每次循环中,当前单元的值被赋给 $value 并且数组内部的指针向前移一步(因此下一次循环中将会得到下一个单元).

    第二种格式做同样的事,只除了当前单元的键名也会在每次循环中被赋给变量 $key.

    自php 5 起,还可能遍历对象.

    注: 当 foreach 开始执行时,数组内部的指针会自动指向第一个单元.这意味着不需要在 foreach 循环之前调用 reset().

    注: 除非数组是被引用,foreach 所操作的是指定数组的一个拷贝,而不是该数组本身.因此数组指针不会被 each() 结构改变,对返回的数组单元的修改也不会影响原数组.不过原数组的内部指针的确在处理数组的过程中向前移动了.假定 foreach 循环运行到结束,原数组的内部指针将指向数组的结尾.

    自php5起,可以很容易地通过在 $value 之前加上 & 来修改数组的单元.此方法将以引用赋值而不是拷贝一个值.

   

    $arr = array(1, 2, 3, 4);

    foreach ($arr as &$value) {

    $value = $value * 2;

    }

    // $arr is now array(2, 4, 6, 8)

    ?>

    此方法仅在被遍历的数组可以被引用时才可用(例如是个变量).

    注意: foreach 不支持用“@”来抑制错误信息的能力.

php循环语句for和foreach的区别

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

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)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

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.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment