PHP学习笔记-->005 PHP语句php入门笔记
1、If...Else 语句
if、elseif 以及 else 语句用于执行基于不同条件的不同动作。
语法
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;2、Switch 语句
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; }原理:
3、while语句
只要指定的条件成立,则循环执行代码块。
语法
while (condition) code to be executed;4、do...while 语句
至少执行一次代码 - 然后,只要条件成立,就会重复进行循环。
语法
do { code to be executed; } while (condition);5、for 语句
如果已经确定了代码块的重复执行次数,则使用 for 语句。
语法
for (initialization; condition; increment) { code to be executed; }for 语句有三个参数。
第一个参数初始化变量,第二个参数保存条件,第三个参数包含执行循环所需的增量。如果 initialization 或 increment 参数中包括了多个变量,需要用逗号进行分隔。而条件必须计算为 true 或者 false。
6、foreach 语句
用于循环遍历数组。每进行一次循环,当前数组元素的值就会被赋值给 value 变量(数组指针会逐一地移动) - 以此类推。
语法
foreach (array as value) { code to be executed; }eg:
<?php $arr=array("one", "two", "three"); foreach ($arr as $value) { echo "Value: " . $value . "<br />"; } ?>