PHP自定义函数
当前实例版本:22 1 评论 814 浏览 发布于:2013年11月14 10:30 编辑+新实例

几乎所有的编程语言的主要特点之一是可按需创建可重用代码的能力。在PHP中,这是通过创建用户定义的函数。在这篇文章中,我将向您展示在PHP中如何定义和使用函数。

创建函数

  • 可以定义函数在PHP脚本中几乎任何地方。一个PHP脚本,这包括全局作用域,在其他函数或在一个类中。
  • 使用关键字function 和 函数名称定义函数。
  • A function may have 0 or more arguments, indicated between parentheses after the function name
  • The code body of a function appears between an opening and closing brace
  • A valid function name begins with a letter or underscore, following by any number of letters, numbers or underscores
  • Unlike variable names, function names are case-insensitive
  • A function is called using the function name and parentheses (which may or may not include arguments between the parentheses)

The following PHP code defines two basic functions: one with an argument and one without. It also shows how to call both functions.

Sample 1. Definition of functions and function call.
    function first()
    {
        echo 
'First';
    }

    function 
second($name)
    {
        echo 
'Second: '.$name;
    }

    
// call the first function
    
first();

    
// call the second function
    
second('some argument');

    
## Output:
    ## ------------------------------
    ## First
    ## Second: some argument 
?>

  • Unlike many other languages, functions do not need to be declared or defined before they are called. Exceptions to this rule: if a function is defined within another function or conditionally defined you can only call that function after it has been created.
  • All functions are created in the global scope. This means even if a function b() was defined in function a()you can still call b() from outside of a()
  • Recursion is supported in PHP, but should be avoided if possible (the PHP manual states a function with over 100-200 levels may cause issues)
Note: There are different issues to be aware of when defining functions (also known as methods) inside classes. This is not covered in this article.

Function Arguments

Data can be passed to PHP functions using arguments. The ability to have default values, variable argument lists and type definitions make functions very powerful.

  • Variables can be passed either by value (a fresh copy of the value inside the function) or by reference (the same variable being used inside and outside of the function)
  • Objects are automatically passed by reference
  • Other types can be passed by reference be preceding the argument name with &

The following code demonstrates the difference between passing by value and passing by reference. The $b value is passed by reference. When it is output at the end you can see its value has been modified.

Sample 2. Passing arguments to function.
    function sampleFunc($byValue, &$byReference){
        
$byValue++;
        
$byReference++;
        
        echo 
sprintf("Inside: %d, %d\n"$byValue$byReference);
    }

    
$a 5;
    
$b 10;

    echo 
sprintf("Before: %d, %d\n"$a$b);

    
sampleFunc($a$b);

    echo 
sprintf("After: %d, %d\n"$a$b);
    
    
## Output:
    ## ------------------------------
    ## Before: 5, 10
    ## Inside: 6, 11
    ## After: 5, 11 
?>

  • You can specify default values for arguments. If the argument is omitted from the function call the default is used
  • The default value must be a constant expression (such as a string, integer, or NULL)
  • You can have multiple arguments with default values, but these should always appear after non-optional arguments

The following code demonstrates how default values can be used:

Sample 3. Using default values.
    function nameAndSize($name$size "small")
    {
        echo 
sprintf("%s is %s\n"$name$size);
    }
 
    
nameAndSize("John");
    
nameAndSize("Paul"null);
    
nameAndSize("George""tall");
    
    
## Output:
    ## ------------------------------
    ## John is small
    ## Paul is
    ## George is tall
?>

Variable-Length Argument Lists

In addition to supporting default values for function arguments, PHP supports variable-length argument lists with functions such as func_get_args().

Regardless of what the function definition dictates, you can retrieve every single value passed to a function usingfunc_get_args(). This function returns an array, where each element corresponds to a single argument. This is useful it you want your function to accept any number of arguments.

The following function demonstrates how you may achieve this. In this example the function definition still requires at least one value to be passed (even though it's not accessed directly).

Tip: Your function documentation should indicate the fact that any number of arguments is accepted, as it's not possible to indicate this using your code (except via run-time error reporting).
Sample 4. Passing hidden arguments.
    function outputUsers($name1)
    {
        foreach(
func_get_args() as $arg){
            echo 
$arg."
"
;
        }
    }
 
    
outputUsers("John""Paul""George""Ringo");

    
## Output:
    ## ------------------------------
    ## John
    ## Paul
    ## George
    ## Ringo
?>

Returning Values

  • You can return a value from a function using the return() keyword
  • If there's no return() executed then NULL is the default return value
  • It is typically good practice to use as a few returns in a function as possible (only one if possible)
  • You can only return a single value from a function, but there are two ways around this
  • First way: return a more complex value such as an array or an object which encapsulates multiple values
  • Second way: use by-reference arguments and update those values in your function

The following listing demonstrates how you can use return values:

Sample 5. How to use return values.
  function foo()
    {
        return 
1234;
    }
 
    function 
bar()
    {
        return array(
12345678);
    }
 
    
$num foo();
    
$arr bar();
?>

The following listing demonstrates more complex usage of return values in conjunction with arguments passed by reference.

Sample 6. Complex usage of return values.
    /**
     * Try to write a value to the passed variable
     *
     * @param   string  &$buffer    The variable to write to
     * @return  bool                True if successful, false if not
     */
    
function writeToBuffer(&$buffer){
        
$ret false;
 
        if(
strlen($buffer) > 0){
            
$buffer .= "some text";
            
$ret true;
        } 
        return 
$ret;
    }
 
    
$buffer "adsf";
 
    if(
writeToBuffer($buffer)){
        echo 
sprintf("Success: %s\n"$buffer);
    }else{
        echo 
"Failure\n";
    }
    
    
## Output:
    ## ------------------------------
    ## Success: adsfsome text 
?>

Anonymous Functions

Since PHP 5.3.0, PHP has supported anonymous functions (or closures). These are primarily useful as a callback for other functions.

The following listing (taken from PHP manual) demonstrates using a closure directly as a callback:

Sample 7. Using a closure directly as a callback.
    $value "hello-world";
 
    echo 
preg_replace_callback("~-([a-z])~", function($match){
        return 
strtoupper($match[1]);
    }, 
$value);
 
    
// outputs helloWorld
?>

You can also assign a closure directly to a PHP variable:

Note: You must include a semi-colon after the closing brace when defining a closure in this manner.

New Features in PHP (from v5.4)

  • Function array dereferencing has been added, e.g. foo()[0].
  • Closures now support $this.

Array dereferencing allows you to do the following.

Sample 8. Array dereferencing.
function fruit()
{
    return array(
"a" => "apple""b" => "banana");
}

echo 
fruit()["a"];
// echoes "apple"

// This roughly translates to using the following code in a current version of PHP.
function fruit()
{
    return array(
"a" => "apple""b" => "banana");
}

$fruits fruit();
echo 
$fruits["a"];
// echoes "apple"

?>