多语言展示
当前在线:286今日阅读:91今日分享:37

如何使用PHP的条件决策

if, elseif ...else 和 switch语句用于采取基于不同的条件决策。可以使用条件语句中的代码。PHP支持以下三种决策语句:if...else statement - 当条件为true时,if 执行一组代码;else 条件不是真的使用此语句;elseif statement - 用于 if...else 语句执行一组代码如果几个条件有一个为真;switch statement - 如果想选择很多代码块来执行,使用switch语句使用。 switch语句是用来避免 if..elseif..else 长的代码块。If...Else 语句如果条件为true,执行一些代码,如果一个条件为假则执行另一个代码,那么使用if.... else语句。
工具/原料
1

php运行环境

2

php编译工具

方法/步骤
1

语法if (condition)  code to be executed if condition is true;else  code to be executed if condition is false;案例一    

2

ElseIf 语句如果想执行一些代码,如果几个条件中的一个为true,那么可以考虑使用elseif语句语法if (condition)  code to be executed if condition is true;elseif (condition)  code to be executed if condition is true;else  code to be executed if condition is false;

3

Switch 语句语法switch (expression) { case label1:   code to be executed if expression = label1;   break;   case label2:  code to be executed if expression = label2;  break; default:  code to be executed  if expression is different  from both label1 and label2;}

4

案例switch语句的工作原理以不一样的方式。首先它计算给定的表达式,然后寻求一个标签,可以匹配所产生的值。如果找到一个匹配值,则具有匹配标签相关的代码会被执行或者如果没有一个标贴的匹配,那么语句将执行任何指定的默认(default)代码。    

5

案例二   ";   echo "Have a nice weekend!";   echo "See you on Monday!";  } ?>  

注意事项

语法结构 逻辑顺序不要错误

推荐信息