包含控制语句
1.include与require
在实际的项目开发中,常常需要根据业务逻辑写一些类或函数保存到独立的文件中,然后再根据需要使用包含语句將相关类文件或函数文件引入
比如有两个文件,一个是error_code.php,一个是包含它的test_include.php文件
erro_code.php文件里定义了两个常量,代码如下


  
  1. <?php
  2. $MY_OK=0;
  3. $MY_ERROR=1;
  4. ?>
  5. test_include.php使用include语句包含文件
  6. <?php
  7. include "erro_code.php";
  8. echo ("MY_OK的值等于$MY_OK\n");
  9. ?>


2.eval()
有些类似于include语句,但是可以动态执行PHP代码


  
  1. <?php
  2. $str='$var=5;';
  3. eval($str);
  4. echo $var;
  5. ?>


3.中止脚本的执行exit()与die()


  
  1. <?php
  2. $var1=12345;
  3. echo $var1;
  4. exit(0);
  5. echo '控制脚本的运行';
  6. ?>
  7. $fp=fopen("./readme.txt","r") or die("不能打开该文件");
  8. //如果fopen打开文件出错,返回布尔值False,die()函数將会立即终止脚本,并显示传递给它的字符串参数


函数
函数可分为系统函数和用户自定义函数

函数的结构
function 函数名称 (参数1,参数2,…,参数N)


  
  1. <?php
  2. function print_table_row(){
  3. echo '<tr><td>表格的内容</td></tr>';
  4. }
  5. echo '<table border="1">';
  6. for($i=0;$i<3;$i++){
  7. print_table_row();
  8. }
  9. echo '</table>';
  10. ?>


2.从函数中返回值return
例子


  
  1. <?php
  2. function print_table_row(){
  3. return '<tr><td>表格的内容</td></tr>';
  4. }
  5. echo '<table border="1">';
  6. for($i=0;$i<5;$i++){
  7. echo print_table_row();
  8. }
  9. echo '</table>';
  10. ?>


3.使用值传递


  
  1. <?php
  2. function print_table_row($bgcolor,$cell){
  3. $cellstring='';
  4. /* if ($bgcolor=='red'){
  5. return;
  6. }*/
  7. for ($i=0;$i<$cell;$i++){
  8. $cellstring .= "<td>$i</td>";;
  9. }
  10. $row="<tr bgcolor=\"$bgcolor\">$cellstring</tr>";
  11. return $row;
  12. }
  13. echo '<table border="1">';
  14. echo print_table_row('gray',3);
  15. echo print_table_row('red',3);
  16. echo print_table_row('gold',3);
  17. echo '</table>';
  18. ?>


4.使用可选参数
例子


  
  1. <?php
  2. function print_table_row($bgcolor,$cell=6){
  3. $cellstring='';
  4. /* if ($bgcolor=='red'){
  5. return;
  6. }*/
  7. for ($i=0;$i<$cell;$i++){
  8. $cellstring .= "<td>$i</td>";;
  9. }
  10. $row="<tr bgcolor=\"$bgcolor\">$cellstring</tr>";
  11. return $row;
  12. }
  13. echo '<table border="1">';
  14. echo print_table_row('gray');
  15. echo print_table_row('red');
  16. echo print_table_row('gold');
  17. echo '</table>';
  18. ?>
  19. -------------随机获取密码
  20. <?php
  21. function genPassword($min=5,$max=8){
  22. $ValidChars = "abcdefghijklmnopqrstuvwxyz123456789";
  23. $max_char = strlen($ValidChars)-1;
  24. $length = mt_rand($min, $max);
  25. $password = "";
  26. for ($i=0;$i<$length;$i++){
  27. $password .= $ValidChars[mt_rand(0, $max_char)];
  28. }
  29. return $password;
  30. }
  31. echo "新的密码 = " . genPassword() . "\n";
  32. echo "新的密码 = " . genPassword(6,10) . "\n";
  33. ?>


5.可变参数的函数
func_num_args() //用于在用户编写的自定义函数中,获取目前传入了几个参数的数量


  
  1. <?php
  2. function getParaNum(){
  3. $arg_num=func_num_args();
  4. echo " 传递的参数数量:".$arg_num;
  5. }
  6. getParaNum("123","13ds","cda");
  7. ?>>
  8. func_get_arg() //指定要存取哪个参数,第一个参数的值为0,可以结合func_num_args()函数自动获取传递的参数值
  9. <?php
  10. function getParaValue($a,$b){
  11. for ($i=0;$i<func_num_args();$i++){
  12. $param=func_get_arg($i);
  13. echo "已取得参数值:$param.<br>";
  14. }
  15. }
  16. getParaValue(1,2,3,4,5,6,7,8,9)
  17. ?>
  18. array func_get_args() //表示將这次传入的参数,以数组的方式返回
  19. <?php
  20. function getParaArray($a,$b){
  21. $param=func_get_args();
  22. $param=explode(", ",$param);
  23. echo "取得的参数为:$param.\n";
  24. }
  25. getParaArray(1,2,3,4,5,6,7,8);
  26. ?>


6.使用引用传递参数


  
  1. <?php
  2. function build_row(&$text){
  3. $text="<tr><td>$text</td></tr>";
  4. }
  5. echo '<table border="1">';
  6. $t='测试用数据';
  7. build_row($t);
  8. echo $t;
  9. echo $t;
  10. echo $t;
  11. echo '</table>';
  12. ?>