PHP流程控制结构

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

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

default:
语句序列 n; break;
}

例1:
week1.php

在这里插入图片描述week2.php
在这里插入图片描述例2:利用下拉菜单,向后端传入一个1-12的数字,分别对应一月到十二月,利用switch进行判断,然后输出对应的月份
month1.php

在这里插入图片描述month2.php
在这里插入图片描述
在这里插入图片描述while循环语句—不确定循环次数的时候用while循环
例1:
//基数为1,每天以0.01的速度增长,一年后的值为多少
$a=1;
i = 1 ; w h i l e ( i=1; while ( i=1;while(i<366) {
a = a= a=a+ a ∗ 0.01 ; / / a*0.01;// a0.01;//a= 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 ; w h i l e ( c=365; while ( c=365;while(c>1) {
b = b= b=b- b ∗ 0.01 ; / / b*0.01;// b0.01;//b= 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 ; w h i l e ( w=1; while ( w=1;while(q>1) {
$w++;
q = q= q=q/2;
}
print_r($w);
print("\n");
例4:
//从一加到一百等于多少
$num=1;
s u m = 0 ; w h i l e ( sum=0; while ( sum=0;while(num<101) {
s u m = 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{
s u m + = 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 {
s u m = 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) {
s u m = 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结合使用
s u m = 0 ; f o r ( 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) {
s u m = sum= sum=i+$sum;
}
}
echo $sum;
print("\n");

例2:定义一个一维数组,使用for循环遍历
a r r 1 = [ 2 , 5 , 7 , " d x c v d " ] ; f o r ( arr1=[2,5,7,"dxcvd"]; for ( arr1=[2,5,7,"dxcvd"];for(i=0; i < c o u n t ( i <count( i<count(arr1) ; $i++) {
echo a r r 1 [ arr1[ arr1[i],",";
}
print("\n");
例3:定义一个二维数组,使用for循环遍历
$arr2=[];
$arr2[0]=[1,2,3];
$arr2[1]=[“ds”,“fee”,“fe”];
a r r 2 [ 2 ] = [ 2 , 4 , " d s " ] ; f o r ( arr2[2]=[2,4,"ds"]; for ( arr2[2]=[2,4,"ds"];for(i=0; i < c o u n t ( i <count( i<count(arr2) ; KaTeX parse error: Expected '}', got 'EOF' at end of input: i++) { for (j=0; j < c o u n t ( j <count( j<count(arr2[$i]) ; $j++) {
echo a r r 2 [ arr2[ arr2[i][$j].",";
}
}
//键值对
一维数组
u s e r = a r r a y ( ′ n a m e ′ = > ′ f u g u i ′ , ′ a g e ′ = > 20 , ′ s e x ′ = > ′ 男 ′ ) ; p r i n t r ( user=array('name'=>'fugui','age'=>20,'sex'=>'男'); print_r( user=array(name=>fugui,age=>20,sex=>);printr(user);
遍历
u s e r 1 = a r r a y ( ′ n a m e ′ = > ′ f u g u i ′ , ′ a g e ′ = > 20 , ′ s e x ′ = > ′ 男 ′ ) ; f o r e a c h ( 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");
}

二维数组–遍历
u s e r 2 = [ [ ′ n a m e ′ = > ′ f u g u i ′ , ′ a g e ′ = > 20 , ′ s e x ′ = > ′ 男 ′ ] , [ ′ n a m e ′ = > ′ c u i h u a ′ , ′ a g e ′ = > 21 , ′ s e x ′ = > ′ 女 ′ ] , [ ′ n a m e ′ = > ′ q i n g q i n g ′ , ′ a g e ′ = > 18 , ′ s e x ′ = > ′ 女 ′ ] , ] ; f o r e a c h ( 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”;
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值