PHP——循环及条件语句

来源:互联网 发布:scala和java混合使用 编辑:程序博客网 时间:2024/06/07 00:03

1.if...else  /  else if

    if 语句 - 如果指定条件为真,则执行代码

if (条件) {  当条件为 true 时执行的代码;}
    if...else 语句 - 如果条件为 true,则执行代码;如果条件为 false,则执行另一端代码

if (条件) {  条件为 true 时执行的代码;} else {  条件为 false 时执行的代码;}
    if...elseif....else 语句 - 选择若干段代码块之一来执行

if (条件) {  条件为 true 时执行的代码;} elseif (condition) {  条件为 true 时执行的代码;} else {  条件为 false 时执行的代码;}

2.switch

    switch 语句 - 语句多个代码块之一来执行

switch (expression){case label1:  如果expression为label1时要执行的代码;  break;case label2:  如果expression为label1时要执行的代码;   break;default:  其他情况下要执行的代码; }

3.while循环

    while - 只要指定条件为真,则循环代码块

while (条件为真) {  要执行的代码;}
    do...while - 先执行一次代码块,然后只要指定条件为真则重复循环

do {  要执行的代码;} while (条件为真);


4.for循环

    for - 循环代码块指定次数

for (赋值语句 ; 判断语句 ; 执行语句) {  要执行的代码;}
    foreach - 循环只适用于数组,并用于遍历数组中的每个元素并循环代码块

foreach ($array as $value) {  要执行的代码}

0 0
原创粉丝点击