当前位置:网站首页>5.PHPwhile,foreach和for循环
5.PHPwhile,foreach和for循环
2022-08-07 07:05:00 【小阿宁的猫猫】
麦穗托着夕阳,晚风吹卷起一串又有一串细碎的光,叶子片片转身,翻出了黄昏。 ——云边有个小卖
PHPwhile,foreach和for循环
1.PHP循环
while - 只要指定的条件成立,则循环执行代码块
do…while - 首先执行一次代码块,然后在指定的条件成立时重复这个循环
for - 循环执行代码块指定的次数
foreach - 根据数组中每个元素来循环代码块
2.while循环
(1) while 循环将重复执行代码块,直到指定的条件不成立
while (条件)
{
要执行的代码;
}
比如:
<?php
$i=1;
while($i<=5)
{
echo "The number is " . $i . "<br>";
$i++;
}
?>
![[外(D:\md图片\1659694853902.png)]](/img/98/f15fe68dcf2a66eef1bedaa30be03b.png)
(2) do…while 语句
do
{
要执行的代码;
}
while (条件);
比如:
<?php
$i=1;
do
{
$i++;
echo "The number is " . $i . "<br>";
}
while ($i<=5);
?>

3.for循环
for 循环用于预先知道脚本需要运行的次数的情况。
for (初始值; 条件; 增量)
{
要执行的代码;
}
比如:
<?php
for ($i=1; $i<=5; $i++)
{
echo "数字为 " . $i . PHP_EOL;
}
?>

4.foreach 循环
用于遍历数组,输出每一组键与值
每一次循环,当前数组元素的键与值就都会被赋值给 $key 和 $value 变量(数字指针会逐一地移动),在进行下一次循环时,将看到数组中的下一个键与值
foreach ($array as $value){
要执行代码;
}
比如:
<?php
$x=array(1=>"Google", 2=>"Baidu", 3=>"Taobao");
foreach ($x as $key => $value)
{
echo "key 为 " . $key . ",对应的 value 为 ". $value . PHP_EOL;
echo "<br>";
}
?>











边栏推荐
- LeetCode 628. Maximum Product of Three Numbers
- WEB安全基础 - - -弱口令和暴力破解
- LNK2001 unresolved external symbol cuGetErrorName resolved
- Comparison and summary of the four methods of DOM, SAX, JDOM, and DOM4J
- VoLTE Basic Self-Learning Series | Which scenarios will trigger CSFB on VoLTE terminals?
- LeetCode sword refers to Offer 09. Two stack is used to implement a queue
- DeFi Prospects: An Overview of Q2 Progress of Mainstream DeFi Protocols
- 【问题记录】一次由filter引发的血案,如何定位上游链路的问题,问题排查与定位思路分享
- Redis 定长队列的探索和实践
- 【C语言】内存函数
猜你喜欢

LeetCode 剑指 Offer 30. 包含min函数的栈

The network layer first

VoLTE basic self-study series | RTP and RTCP protocol principles

Qt 使用SQLite的性能优化的亿点点记录

grid网格布局

Completed - based on SSM online movie booking system

HPC Technology: Analysis of MPICH Implementation Principle

Top 20 most popular plugins for QGIS

bp神经网络 损失函数,bp神经网络参数优化

好消息|Erda 加入中国开源社区 landscape
随机推荐
VoLTE Basic Self-Learning Series | What is Forking in SIP and IMS
FPGA开发第二弹:流水灯实验
VoLTE Basic Self-Learning Series | VoLTE Network Architecture
31. Understand what is avalanche, penetration and breakdown of redis?What happens after redis crashes?What are the countermeasures
服务端获取用户ip
图论与网络模型——基于R
微信小程序--》小程序全局配置和详解下拉刷新和上拉触底页面事件
ASEMI整流桥GBL610参数,GBL610尺寸,GBL610特征
ansible当中模块的使用
Network interconnection model, physical layer, data link layer
Ali cloud message service, SMS
MySQL - 索引优化
【n-piece chess】
Web3 Reference Architecture
CTO强烈禁止使用Calendar,那用啥?
传输层(UDP协议,TCP协议三次握手、四次挥手)
In-depth analysis of HyperBDR cloud disaster recovery 2: Self-developed Boot in Cloud technology to achieve highly automated cloud disaster recovery
PriorityQueue
5 Crypto Themes Poised to Lead the Next Bull Market
排序--选择排序