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;//
a∗0.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;//
b∗0.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”;
}
巢状条件分支结构
嵌套循环语句