互联网程序设计·PHP语言之流程控制
第1关 条件语句 – 学生测评
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312' />
</head>
<body>
<?php
$score=99;
echo "if: ";
/***********Begin************/
//Õⲿ·ÖÇëÀûÓÃifÓï¾äÍê³ÉÈÎÎñ
if($score < 70){
echo "该学生为不及格!";
}else if($score >=70 && $score < 80){
echo "该学生为及格!";
}else if($score >=80 && $score < 90){
echo "该学生为良好!";
}else if($score > 90){
echo "该学生为优秀!";
}
/***********End************/
echo "<br>";
echo "switch: ";
/************Begin**************/
//Õⲿ·ÖÇëÀûÓÃswitchÓï¾äÍê³ÉÈÎÎñ
switch($score){
case $score <60: echo "该学生为不及格!" ;break;
case $score >=60 && $score<80: echo "该学生为及格!" ;break;
case $score >=80 && $score<90: echo "该学生为良好!" ;break;
case $score >=90 && $score<=100: echo "该学生为优秀!";break;
//default: echo "该学生为不及格!";break;
}
/************End************/
?>
</body>
</html>
第2关 do-while语句 – 辗转相除法
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312' />
</head>
<body>
<?php
$num1=10;
$num2=5;
/***********Begin*******/
do{
$t = $num1 % $num2;
$num1 = $num2;
if($t) $num2 = $t;
}while( $t );
echo "这两个数的最大公约数为:".$num2;
/***********End************/
?>
</body>
</html>
第3关 for语句 – 打印三角形
<html>
<head>
<meta http-equiv='Content-Type' content='text/html; charset=gb2312' />
</head>
<body bgcolor="#3498db">
<?php
$n=4;
/**********Begin*********/
for($i=1;$i<=$n;$i++){
for($k=1;$k<=$n-$i;$k++){
echo " ";
}
for($j=1;$j<=$i*2-1;$j++){
echo "*";
}
echo "\n";
}
/*******End******/
?>
</body>
</html>
701 Views