PHP  
downloads | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
search for in the  
<include_once函数的参数>
Last updated: Mon, 16 Jul 2012

章 17. 函数

用户自定义函数

一个函数可由以下的语法来定义:

例子 17-1. 展示函数用途的伪码

<?php
function foo ($arg_1, $arg_2, ..., $arg_n)
{
   echo
"Example function.\n";
   return
$retval;
}
?>

任何有效的 PHP 代码都有可能出现在函数内部,甚至包括其它函数和 定义。

在 PHP 3 中,函数必须在被调用之前定义。而 PHP 4 则不再有这样的 条件。除非函数如以下两个范例中有条件的定义。

如果一个函数以以下两个范例的方式有条件的定义,其定义必须在调用之前完成。

例子 17-2. 有条件的函数

<?php

$makefoo
= true;

/* We can't call foo() from here
   since it doesn't exist yet,
   but we can call bar() */

bar();

if (
$makefoo) {
  function
foo ()
  {
   echo
"I don't exist until program execution reaches me.\n";
  }
}

/* Now we can safely call foo()
   since $makefoo evaluated to true */

if ($makefoo) foo();

function
bar()
{
  echo
"I exist immediately upon program start.\n";
}

?>

例子 17-3. 函数中的函数

<?php
function foo()
{
  function
bar()
  {
   echo
"I don't exist until foo() is called.\n";
  }
}

/* We can't call bar() yet
   since it doesn't exist. */

foo();

/* Now we can call bar(),
   foo()'s processesing has
   made it accessable. */

bar();

?>

PHP 不支持函数重载,可能也不支持取消定义或者重定义已声明的函数。

注: 函数名是非大小写敏感的,不过在调用函数的时候,通常使用其在定义时相同的形式。

PHP 3 虽然支持默认参数(更多的信息请参照 默认参数的值) ,但是却不支持可变的参数个数。 PHP 4 支持: 见 可变长度的参数列表 和涉及到的相关函数 func_num_args()func_get_arg(), 和 func_get_args() 以获取更多的信息。




<include_once函数的参数>
 Last updated: Mon, 16 Jul 2012
 
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: http://manual.phpv.net/
Last updated: Thu Jul 7 19:13:47 2005 CST