if else语句
if(条件表达式){
语句组1
}else{
语句组2
//语句组为单条语句时可省略“{ }”。
}

switch-case语句语法
switch(表达式){
case 值1:
语句序列1; break;
case 值2:
语句序列2; break;

default:
语句序列 n; break;
}

例1:
week1.php

PHP流程控制结构week2.php
PHP流程控制结构例2:利用下拉菜单,向后端传入一个1-12的数字,分别对应一月到十二月,利用switch进行判断,然后输出对应的月份
month1.php

PHP流程控制结构month2.php
PHP流程控制结构
PHP流程控制结构while循环语句—不确定循环次数的时候用while循环
例1:
//基数为1,每天以0.01的速度增长,一年后的值为多少
$a=1;
i=1;while(i=1; while (i<366) {
a=a=a+a0.01;//a*0.01;//a=a+a+a*1.01
KaTeX parse error: Expected 'EOF', got '}' at position 7: i++; }̲ print_r(a);
print("\n");

例2:
//基数为1,每天以0.01的速度减少,一年后的值为多少
$b=1;
c=365;while(c=365; while (c>1) {
b=b=b-b0.01;//b*0.01;//b=b+b+b*1.01
KaTeX parse error: Expected 'EOF', got '}' at position 7: c--; }̲ print_r(b);
print("\n");
例3:
// 有十个亿,一天花一半,问多少天花完
$q=10000000000;
w=1;while(w=1; while (q>1) {
$w++;
q=q=q/2;
}
print_r($w);
print("\n");
例4:
//从一加到一百等于多少
$num=1;
sum=0;while(sum=0; while (num<101) {
sum=sum=sum+$num;
KaTeX parse error: Expected 'EOF', got '}' at position 9: num++; }̲ print(sum);

do while循环语句–不管满不满足条件,都会执行一次循环
例1:求1到100的累加。
<?php
KaTeX parse error: Unexpected character: '' at position 7: i=1; ̲sum=0;
do{
sum+=sum+=i;
KaTeX parse error: Expected 'EOF', got '}' at position 8: i++; }̲while(i<=100);
echo $sum;
?>
例2: 1-100之间的奇数和
第一种方法:
$num=1;
$sum=0;
do {
sum=sum=sum+$num;
KaTeX parse error: Expected 'EOF', got '}' at position 11: num+=2; }̲ while (num<=100);
echo $sum;
print("\n");
?>
第二种方法:
$num=1;
KaTeX parse error: Expected '}', got 'EOF' at end of input: … do { if (num%2==0) {
sum=sum=sum+$num;
}
KaTeX parse error: Expected 'EOF', got '}' at position 11: num++; }̲ while (num<=100);
echo $sum;
print("\n");

for循环语句
for(表达式1;表达式2;表达式3)
{
语句或语句序列;
}

例1:使用for循环求1-100的偶数和,要求和if结合使用
sum=0;for(sum=0; for (i=0; $i < 101; KaTeX parse error: Expected '}', got 'EOF' at end of input: i++) { if (i%2==0) {
sum=sum=i+$sum;
}
}
echo $sum;
print("\n");

例2:定义一个一维数组,使用for循环遍历
arr1=[2,5,7,"dxcvd"];for(arr1=[2,5,7,"dxcvd"]; for (i=0; i<count(i <count(arr1) ; $i++) {
echo arr1[arr1[i],",";
}
print("\n");
例3:定义一个二维数组,使用for循环遍历
$arr2=[];
$arr2[0]=[1,2,3];
$arr2[1]=[“ds”,“fee”,“fe”];
arr2[2]=[2,4,"ds"];for(arr2[2]=[2,4,"ds"]; for (i=0; i<count(i <count(arr2) ; KaTeX parse error: Expected '}', got 'EOF' at end of input: i++) { for (j=0; j<count(j <count(arr2[$i]) ; $j++) {
echo arr2[arr2[i][$j].",";
}
}
//键值对
一维数组
user=array(name=>fugui,age=>20,sex=>);printr(user=array('name'=>'fugui','age'=>20,'sex'=>'男'); print_r(user);
遍历
user1=array(name=>fugui,age=>20,sex=>);foreach(user1=array('name'=>'fugui','age'=>20,'sex'=>'男'); foreach (user1 as $key => KaTeX parse error: Expected '}', got 'EOF' at end of input: …e) { print_r(value."\n");
}

二维数组–遍历
user2=[[name=>fugui,age=>20,sex=>],[name=>cuihua,age=>21,sex=>],[name=>qingqing,age=>18,sex=>],];foreach(user2=[ ['name'=>'fugui','age'=>20,'sex'=>'男'], ['name'=>'cuihua','age'=>21,'sex'=>'女'], ['name'=>'qingqing','age'=>18,'sex'=>'女'], ]; foreach (user2 as $key => KaTeX parse error: Expected '}', got 'EOF' at end of input: …) { foreach (value as $key1 => KaTeX parse error: Expected '}', got 'EOF' at end of input: …e1) { print(value1.",");
}
echo “\n”;
}

巢状条件分支结构
嵌套循环语句

相关文章: