博主信息
博文 42
粉丝 0
评论 1
访问量 32486
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
for、while、foreach的使用方法-2018年4月18日20:10完成
邵军-山东-84918的博客
原创
737人浏览过

差一点没有完成!汗,不熟练,唯有苦学!IMG_20180418_201852_看图王.jpg

实例

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <title>for、while、foreach遍历方法</title>
</head>
<body>
<h2>索引数组0到10的累加和计算方法</h2>
<button type="">for方法求和</button>
<button type="">while方法求和</button>
<button type="">foreach方法求和</button>
<hr color="red">
<h2>关联数组的遍历方法</h2>
<button type="">for方法遍历</button>
<button type="">while方法遍历</button>
<button type="">foreach方法遍历</button>
<?php
$sum=0;
$a=array();
for($i=0;$i<=10;$i++)
{
   array_push($a,$i);
$sum+=$a[$i];
}
$total=0;
$b=array();
$j=0;
while($j<=10)
{
array_push($b,$j);
$total+=$b[$j];
$j++;
}
$totl=0;
$c=array("0","1","2","3","4","5","6","7","8","9","10");
foreach ($c as $value)
{
    $totl+=$value;
}
echo'<hr color="red">';

$e='';
    $d=array("no1"=>"VOLVO","no2"=>"BENZ","no3"=>"BMW");
for($i=1;$i<=count($d);$i++){
     $e.=key($d).'=>'.current($d).' ';
next($d);
}


$m='';
$h=0;
reset($d);
$d=array("no1"=>"VOLVO","no2"=>"BENZ","no3"=>"BMW");
while($h<=count($d)){
$m.=key($d).'=>'.current($d).' ';
next($d);
$h++;
}
$n='';
foreach ($d as $key => $value) {
   $n.=$key .'=>'. $value.' ';
}

 ?>
<script type="text/javascript" src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>

<script type="text/javascript">
     $('button').eq(0).click(function(){
alert("<?php echo('for方法求和:'.$sum); ?>");
     })
     $('button').eq(1).click(function(){
alert("<?php echo('while方法求和:'.$total); ?>");
     })
     $('button').eq(2).click(function(){
alert("<?php echo('foreach方法求和:'.$totl); ?>");
     })
     $('button').eq(3).click(function(){
alert("<?php echo('for方法遍历:'.$e); ?>");})
     $('button').eq(4).click(function(){
alert("<?php echo('while方法遍历:'.$m); ?>");
     })
     $('button').eq(5).click(function(){
alert("<?php echo('foreach方法求和:'.$n); ?>");
     })


</script>
</body>
</html>

运行实例 »

点击 "运行实例" 按钮查看在线实例


批改状态:合格

老师批语:
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学