标签:
根据表单提交的数据用户在浏览器生成图一示例的图形。
图形参数说明:
大小:图形中最长那一行的字符数。
奇数行/偶数行:最长那一个行为奇数行,紧邻的两行算偶数行,以此类推。
案例效果图:
应用了for循环,还应用了一个php函数 str_repeat
str_repeat() 函数把字符串重复指定的次数。
用法:有两个参数,
str_repeat(string,repeat)string必需。规定要重复的字符串。
repeat必需。规定字符串将被重复的次数。必须大于等于 0。
代码如下:
<?php /* 循环的一个小效果 */ header('content-type:text/html;charset=utf8'); if(!empty($_POST)) { $qi=$_POST['qi']; $ou=$_POST['ou']; $count=$_POST['size']; for ($i=0; $i <= $count; $i++) { $num1=str_repeat("$qi",$i)."<br>"; $num2=str_repeat("$ou",$i)."<br>"; if($i%2==0) { echo $num2; }else{ echo $num1; } } for ($i=($count-1); $i >= 0 ; $i--) { $num1=str_repeat("$qi",$i)."<br>"; $num2=str_repeat("$ou",$i)."<br>"; if($i%2==0) { echo $num2; }else{ echo $num1; } } } ?> <form action="xunhuan.php" method="post"> 大小:<input type="text" name="size" style="width:50px"> 奇数行:<input type="text" name="qi" style="width:50px"> 偶数行:<input type="text" name="ou" style="width:50px"> <input type="submit" value="生成图形"> </form>
标签:
原文地址:http://blog.csdn.net/json_ligege/article/details/51336967