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<366) {
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>1) {
b-b=b*1.01
KaTeX parse error: Expected 'EOF', got '}' at position 7: c--;
}̲
print_r(b);
print("\n");
例3:
// 有十个亿,一天花一半,问多少天花完
$q=10000000000;
q>1) {
$w++;
q/2;
}
print_r($w);
print("\n");
例4:
//从一加到一百等于多少
$num=1;
num<101) {
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{
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+$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+$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结合使用
i=0; $i < 101; KaTeX parse error: Expected '}', got 'EOF' at end of input: i++) {
if (i%2==0) {
i+$sum;
}
}
echo $sum;
print("\n");
例2:定义一个一维数组,使用for循环遍历
i=0; arr1) ; $i++) {
echo i],",";
}
print("\n");
例3:定义一个二维数组,使用for循环遍历
$arr2=[];
$arr2[0]=[1,2,3];
$arr2[1]=[“ds”,“fee”,“fe”];
i=0; arr2) ; KaTeX parse error: Expected '}', got 'EOF' at end of input: i++) {
for (j=0; arr2[$i]) ; $j++) {
echo i][$j].",";
}
}
//键值对
一维数组
user);
遍历
user1 as $key => KaTeX parse error: Expected '}', got 'EOF' at end of input: …e) {
print_r(value."\n");
}
二维数组–遍历
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”;
}
巢状条件分支结构
嵌套循环语句