Saturday 30 July 2011

PHP Development with Cat Technologies

PHP A to ZCE: Functions

victor.cattechnologies@gmail.com
 
This article is part of the series “PHP A to Zend Certified Engineer”. In PHP A to ZCE, I will take you through 26 different yet equally important topics that will help you become a Zend Certified Engineer. Even if you're not interested in sitting the ZCE-PHP exam, these topics will elevate your understanding of PHP to a whole new level and allow you to become the guru in your company. Read more about PHP A to Zend Certified Engineer...
One of the key features of nearly all programming languages is the ability to create reusable code that can be used on-demand. In PHP this is achieved with the creation of user-defined functions. In this article I will show you how to define and use functions in PHP.

Creating Functions

  • Functions can be defined at nearly any point in a PHP script. This includes the global scope of a PHP script, within another function, or within a class.
  • Functions are defined using the keyword function followed by the function name
  • 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.
Listing 1 listing-1.php
<?php
    function first()
    {
        echo 'First';
    }
 
    function second($name)
    {
        echo 'Second: ' . $name;
    }
 
    // call the first function
    first();
 
    // call the second function
    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 covered in the PHP A to ZCE article on Object-Oriented Programming.

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.
  • 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:
Listing 2 listing-2.php
<?php
    function nameAndSize($name, $size = 'small')
    {
        echo sprintf("%s is %s\n", $name, $size);
    }
 
    nameAndSize('John');
    nameAndSize('Paul', null);
    nameAndSize('George', '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 using func_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)
Listing 3 listing-3.php
<?php
    function outputUsers($name1)
    {
        foreach (func_get_args() as $arg) {
            echo $arg . "\n";
        }
    }
 
    outputUsers('John', 'Paul', 'George', 'Ringo');?>

Returning Values

  • You can return a value from an array 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:
Listing 4 listing-4.php
<?php
    function foo()
    {
        return 1234;
    }
 
    function bar()
    {
        return array(1234, 5678);
    }
 
    $num = foo();
    $arr = bar();?>
The following listing demonstrates more complex usage of return values in conjunction with arguments passed by reference.
Listing 5 listing-5.php
<?php
    /**
     * 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";
    }
?>

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:
Listing 6 listing-6.php
<?php
    $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.