PHP A to ZCE: Functions
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
functionfollowed 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.
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)
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:
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).
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
NULLis 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:
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.
/** * 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:
$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:
Further Reading
- PHP manual entry on functions
- PHP manual entry on anonymous functions
- PHP manual entry on references
Other Options
- Download a PDF version of this article
- Put your PHP knowledge to the test with our online and iPad/iPhone quizzes
- View or post comments for this article
- Browse similar articles by tag: PHP, ZCE
- Read related articles:




