background image

// execute this if no break statement

// has been encountered hitherto
}
3)        ?操作符:

( expression )?returned_if_expression_is_true:returned_if_expression_is_false;
4)        while 语句:

(1  

) while ( expression ) 

{
              // do something

}

(2)do
{

// code to be executed
} while ( expression );
5)        for 语句:

for ( initialization expression; test expression; modification expression ) {
// code to be executed

}
6)        break;continue
9   

编写函数

1)        定义函数:
function function_name($argument1,$argument2,……) //形参
{

//function code here;
}
2)        函数调用
function_name($argument1,$argument2,……); //形参
3)        动态函数调用(Dynamic Function Calls):

<html>
<head>

<title>Listing 6.5</title>
</head>

<body>
<?php
function sayHello() {   //定义函数 sayHello

print "hello<br>";
}
$function_holder = "sayHello";  //将函数名赋值给变量$function_holder
$function_holder();  //变量$function_holder 成为函数 sayHello 的引用,调用
$function_holder()相当于调用 sayHello

?>
</body>

</html>
4)        变量作用域:

全局变量:
<html>
<head>

<title>Listing 6.8</title>
</head>

<body>
<?php

$life=42;
function meaningOfLife() {

global $life;
/*在此处重新声明$life 为全局变量,在函数内部访问全局变量必须这样,如果在函数内改变变量的值,

将在所有代码片段改变*/
print "The meaning of life is $life<br>"; 

}
meaningOfLife();

?>
</body>

</html>