正文

函数参数传递的方式2009-07-29 18:57:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/sovf/45812.html

分享到:

函数参数传递的方式有两种:
1
、传值方式。缺省情况下,函数参数通过传值的方式传递,因此即使在函数内部改变参数的值,它并不会改变函数外部参数的值。
2、传址方式。传址时只需在函数调用时在参数的前面加上“&”号即可。将函数外部的值的内存地址传递给内部的参数,在函数内部的所有操作都会改变函数外部参数的值。所以希望函数修改外部参数的值,必须使用传址传址方式。

  1. <?php
  2. //定义一个函数
  3. function f3($a){
  4. $a++;
  5. }
  6. $x=1;
  7. f3($x);
  8. echo "x=$x";//传值方式调用函数
  9. $x=1;
  10. f3(&$x);
  11. echo "x=$x";//传址方式调用函数
  12. ?>
  13. <?php
  14. function add_some_extra(&$string)
  15. {
  16. $string .= 'and something extra.';
  17. }
  18. $str = 'This is a string, ';
  19. add_some_extra($str);
  20. echo $str; // 输出'This is a string, and something extra.'
  21. ?>
  1. <?php
  2. /* 在PHP中,函数不需要在被调用之前定义,在调用后才进行定义也是允许的。
  3. 在少数情况下,函数在需要一定的判断条件下,才能被定义。这样函数的定义必须在函数被调用之前完成。 */
  4. $makefoo = true;
  5. bar(); /*你不能调用foo()函数,它在这里不存在。但是能够调用bar(),调用之后在后面进行定义即可。*/
  6. if ($makefoo) {
  7. function foo()
  8. {
  9. echo "foofoo";
  10. }
  11. }
  12. if ($makefoo) foo(); /* 现在我们可以正常调用foo(),因为只有$makefoo为true和定义了foo()函数后,foo()函数才存在。 */
  13. function bar()
  14. {
  15. echo "barbar";
  16. }
  17. ?>
  18. <?php
  19. function foo()
  20. {
  21. function bar()
  22. {
  23. echo "I don't exist until foo() is called. ";
  24. }
  25. }
  26. /* 这里不能调用bar(),因为它不存在。 */
  27. foo();
  28. /* 现在我们可以调用bar(),只有在调用foo()后,bar()才存在。 */
  29. bar();
  30. ?>

为函数指定默认参数的值

  1. <?php
  2. function test_defaultargs($arg="default value"){
  3. echo "参数值为:".$arg."<br>";
  4. }
  5. test_defaultargs();
  6. test_defaultargs("new value");
  7. ?>
  8. <?php
  9. function makecoffee($type = "cappuccino")
  10. {
  11. return "Making a cup of $type. ";
  12. }
  13. echo makecoffee();
  14. echo makecoffee("espresso");
  15. ?>

请注意当使用默认参数时,任何默认参数必须放在任何非默认参数的右侧;否则,可能函数将不会按照预期的情况运行。

  1. <?php
  2. function makeyogurt($type = "acidophilus", $flavour)
  3. {
  4. return "Making a bowl of $type $flavour. ";
  5. }
  6. echo makeyogurt("raspberry"); // 这个例子将不会按照我们预期的情况运行。
  7. ?>
  8. <?php
  9. function makeyogurt($flavour, $type = "acidophilus")
  10. {
  11. return "Making a bowl of $type $flavour. ";
  12. }
  13. echo makeyogurt("raspberry"); // 这个例子的输出是:Making a bowl of acidophilus raspberry.
  14. ?>

函数名可变

  1. <?php
  2. function f1(){
  3. echo "这是函数f1()。<br>";
  4. }
  5. function f2(){
  6. echo "这是函数f2()。<br>";
  7. }
  8. $var1="f1";
  9. $var1(); //调用函数f1()
  10. $var1="f2";
  11. $var1(); //调用函数f2()
  12. //注意:调用可变函数名需要在变量前加$。
  13. ?>
  14. <?php
  15. function foo() {
  16. echo "foofoo.<br>";
  17. }
  18. function bar($arg = '') {
  19. echo "barbar'$arg'.<br>";
  20. }
  21. function echoit($string)
  22. {
  23. echo $string;
  24. }
  25. $func = 'foo';
  26. $func(); // 调用foo()
  27. $func = 'bar';
  28. $func('test'); // 调用bar()
  29. $func = 'echoit';
  30. $func('test'); // 调用echoit()
  31. ?>

函数可变长度参数

  1. <?php
  2. //向函数传递数组
  3. function takes_array($input)
  4. {
  5. echo "$input[0] + $input[1] = ", $input[0]+$input[1];
  6. }
  7. ?>

func_num_args() -- 返回传递给函数的参数的数量

  1. <?php
  2. function foo()
  3. {
  4. $numargs = func_num_args();
  5. echo "Number of arguments: $numargs .'<br>'";
  6. }
  7. foo(1, 2, 3);
  8. ?>

func_get_arg() -- 从参数列表中返回一个参数值

  1. <?php
  2. function foo()
  3. {
  4. $numargs = func_num_args();
  5. echo "Number of arguments: $numargs .'<br>'";
  6. if ($numargs >= 2) {
  7. echo "Second argument is: " . func_get_arg(1) ."<br>";
  8. }
  9. }
  10. foo (1, 2, 3);
  11. ?>

func_get_args() -- 返回一个包含函数参数的数组

  1. <?php
  2. function foo()
  3. {
  4. $numargs = func_num_args();
  5. echo "Number of arguments: $numargs .'<br>'";
  6. if ($numargs >= 2) {
  7. echo "Second argument is: " . func_get_arg(1) . "<br>";
  8. }
  9. $arg_list = func_get_args();
  10. for ($i = 0; $i < $numargs; $i++) {
  11. echo "Argument $i is: " . $arg_list[$i] . "<br>";
  12. }
  13. }
  14. foo(1, 2, 3);
  15. ?>

阅读(3986) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册