Conditional statements for beginners to PHP

if Judgment statement

Format: if (condition){

Execution code

}

<?php
	header("Content-type: text/html; charset=utf-8");//设置编码
	$a = 15;
	if($a==15){
		echo "满足条件";
	}
	
	//注:条件判断的时候,不要写一个等号  一个等号是赋值
?>

if...else Statement

Format if (condition){

Code block 1;

}else{

Code Block 2;

}

<?php
	header("Content-type: text/html; charset=utf-8");//设置编码
	//if....else
	//1代表 北京   0代表上海
	$i=0;

	if($i==1){
		echo "欢迎来到北京";
	}else{
		echo "欢迎来到上海";
	}
?>

Note: Give a variable and assign a value equal to 0 Determine whether $i is equal to 1 If equal, output the first echo statement, otherwise output the second echo statement

if...else if...else


Format: if (condition 1){

Code block 1;

}else if(condition 2){

Code block 2;

}else{

Code block 3;

}

<?php
	header("Content-type: text/html; charset=utf-8");//设置编码
	//判断一个人的考试成绩
	//60以下不及格
	//70-80之间良好
	//80-90之间非很好
	//90-100之间优秀

	$a = 90;
	if($a<60){
		echo "不及格";
	}else if($a>=60 and $a<80){
		echo "良好";
	}else if($a>=80 and $a<90){
		echo "非常好";
	}else{
		echo "优秀";
	}
?>


##switch statement


Format:

$a = 2;

switch($a){

case 1: Execute code 1; break;

case 2: execute code 2; break;

case 3: execute code 3; break;

case 4: execute code 4; break;

default: execute code;

}

<?php
header("Content-type: text/html; charset=utf-8");//设置编码
//判断一个人是年纪大小的状况

$a = 50;
switch ($a) {
	case 20:echo "少年";break;
	case 30:echo "青年";break;
	case 40:echo "中年";break;
	case 50:echo "中老年";break;
	default:echo "老年";
}

?>


break jump

In switch, when the break statement is encountered, Instead of executing downward, realize the jump



##

Continuing Learning
||
<?php header("Content-type: text/html; charset=utf-8");//设置编码 $a = 15; if($a==15){ echo "满足条件"; } ?>
submitReset Code