博客列表 >for、while、foreach的使用方法-2018年4月18日20:10完成

for、while、foreach的使用方法-2018年4月18日20:10完成

邵军-山东-84918的博客
邵军-山东-84918的博客原创
2018年04月18日 20:22:12558浏览

差一点没有完成!汗,不熟练,唯有苦学!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 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议