这是一个用户自定义回调函数的实例
首先建立两个函数exclaim 和ask 函数对变量$str 进行加工处理,一个给字符串添加感叹号,一个给字符串添加问号
再建立一个函数printformatted ,设置两个变量$str ,$format,来接收printformatted函数值,再返回给exclaim 和ask 函数进行加工处理,然后再进行输出。
1 <!DOCTYPE html>
2 <html>
3 <body>
4
5 <?php
6
7 function exclaim($str){
8
9 return $str ."!"; // 对字符串进行加感叹号
10
11 }
12
13 function ask($str){
14
15 return $str ."?"; // 对字符串进行加问号
16
17 }
18
19 function printformatted($str,$format){
20 //调用$format 回调函数
21 echo $format($str);
22 }
23 printformatted("hello word","exclaim");
24 echo "<br>";
25 printformatted("hello word","ask");
26
27 ?>
28
29 </body>
30 </html>
返回结果
hello word!
hello word?
评论