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):
1: <html>
2: <head>
3: <title>Listing 6.5</title>
4: </head>
5: <body>
6: <?php
7: function sayHello() { //定义函数sayHello
8: print "hello<br>";
9: }
10: $function_holder = "sayHello"; //将函数名赋值给变量$function_holder
11: $function_holder(); //变量$function_holder成为函数sayHello的引用,调用$function_holder()相当于调用sayHello
12: ?>
13: </body>
14: </html>
4)变量作用域:
全局变量:
1: <html>
2: <head>
3: <title>Listing 6.8</title>
4: </head>
5: <body>
6: <?php
7: $life=42;
8: function meaningOfLife() {
9: global $life;
/*在此处重新声明$life为全局变量,在函数内部访问全局变量必须这样,如果在函数内改变变量的值,将在所有代码片段改变*/
10: print "The meaning of life is $life<br>";
11: }
12: meaningOfLife();
13: ?>
14: </body>
15: </html>
5)使用static
1: <html>
2: <head>
3: <title>Listing 6.10</title>
4: </head>
5: <body>
6: <?php
7: function numberedHeading( $txt ) {
8: static $num_of_calls = 0;
9: $num_of_calls++;
10: print "<h1>$num_of_calls. $txt</h1>";
11: }
12: numberedHeading("Widgets"); //第一次调用时,打印$num_of_calls值为1
13: print("We build a fine range of widgets<p>");
14: numberedHeading("Doodads"); /*第一次调用时,打印$num_of_calls值为2,因为变量是static型的,static型是常驻内存的*/
15: print("Finest in the world<p>");
16: ?>
17: </body>
18: </html>