flow control 流程控制
decision structure 判断结构
loop structure 循环结构
if(condition)
{
statement1;
}
if()
{}
else
{}
if()
{}
elseif()
{}
elseif()
{}
else
{}
switch(expression)
{
case value1:
statement1;
break;
case value2:
statement2:
break;
default:
statementsN+1;
}
for(initalizers;expression;iterators)
{
statements;
[break;]
statements;
}
Examples
for($i=1;$i<=10;$i++)
{
echo $i.'<br>';
}
条件循环 conditional loops
while(condition)
{
statements;
[break;]
statements;
}
$i=1
while($i<=10)
{
echo $i++.'<br>';
}
do while
do
{
statements;
[break;]
statements;
}
while();
$i=1;
do
{
echo $i++.'<br>';
}
while($i<=10);
break语句 强制离开循环 for while do while
continue 循环内跳过后面的语句
exit()函数 强制终止程序并在网页上显示字符串
foreach
foreach(array_name as $value)
{
statements;
[break;]
statements;
}
foreach(array_name as $key=>$value)
{
statements;
[break;]
statements;
}
array 数组
$arr[0] ='兰花' ;
$arr['花名'] ='兰花' ;
$arr[1][2] ='玫瑰' ; //二维数组
$arr['flower']['red'] = '玫瑰' ; //二维数组
$my_array[0] = 100;
$my_array = array();
$my_array = array('北京','上海','深圳');
$my_array = array('CN'=>'中国','US'=>'美国','CA'=>'加拿大');
$my_array[]=100;//默认的第一个键为0
使用list函数存取一维数组
$my_array = array('CN'=>'中国','US'=>'美国','CA'=>'加拿大');
list($tour1,$tour2) = $my_array;
echo $tour1; //中国
echo $tour2; //美国
数组运算符 + == === !=
数组函数 is_array count in_array unset current pos next prev end reset array_walk each list array_combine array_diff ...