Functions in PHP

This tutorial describes what is functions in PHP and how to use them.
Provided by Quentin Zervaas

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.

Sample 1. Definition of functions and function call.
<?php
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.
<?php
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.
<?php
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 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).
Sample 4. Passing hidden arguments.
<?php
function outputUsers($name1)
{
    foreach(
func_get_args() as $arg){
        echo 
$arg."<br>";
    }
}

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.
<?php
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.
<?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";
}

## 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.
<?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.

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.
<?php
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"

?>

Comments


Please post only comments related to the original tutorial. Be polite and helpful, do not spam or offend others.
Create Your Free Account
Please remember that this information is essential to use our services correctly.
After creating the account you will be able to download all of our FREE products.
Fields marked with * are mandatory






Please send me information about updates, new products, specials and discounts from ApPHP!
We recommend that your password should be at least 6 characters long and should be different from your username/email. Please use only letters of the English alphabet to enter your name.

Your e-mail address must be valid. We use e-mail for communication purposes (order notifications, etc). Therefore, it is essential to provide a valid e-mail address to be able to use our services correctly.

All your private data is confidential. We will never sell, exchange or market it in any way. Please refer to Privacy Policy.

By clicking "Create Account", you are indicating that you have read and agree to the ApPHP Terms & Conditions.