Organize Code

Function

PHP

 

Defining Function

PHP 5
function greet(){
    print("Hello!");
}

greet();  // Hello!

Function is a block of code with a specific purpose that we encapsulate and run as many times as we want.

Functions are not run by themselves, and we should invoke (call) them to execute them.

Function can optionally return something one-time using the return keyword.

 

Arguments

PHP 5
function add($a, $b){
    return $a + $b;
}

$val1 = add(2, 3);  // 5
$val2 = add(3, 4);  // 7

Functions may receive inputs, which are called Arguments.

 

Anonymous Function

PHP 5
$sum = function($a, $b){
    return $a + $b;
};

$val = $sum(2, 3);  // 5

This is another way of defining functions in PHP. These functions are called Anonymous or Lambda.

Here we are creating an anonymous function in the runtime and assign it to a variable $sum. From here you can call that anonymous function using variable $sum.

 

Default Argument

PHP 5
function sum($a, $b=1){
    return $a + $b;
}

$val1 = sum(2);     // 3
$val2 = sum(2, 3);  // 5

Default Arguments are arguments that have a value assigned to them by default, so whenever that argument is missing in the function call, that default value will be used.

Default Arguments should be the right most arguments in the function.

 

Arguments by Reference

PHP 5
function greet1($name){
    $name = 'John';
    print('Hello ' . $name);
}

function greet2(&$name){
    $name = 'John';
    print('Hello ' . $name);
}


$name = 'Sam';

greet1($name);  // Hello John
print($name);   // Sam

greet2($name);  // Hello John
print($name);   // John

By default, function arguments are passed by value meaning if the value of an argument is changed inside the function, it does not change outside of the function.

To allow a function to modify its arguments, they must be passed by reference. This should be specified when we are defining the function using & before the argument name.

In this example:

  • greet1 parameter is passed by value.
  • greet2 parameter is passed by reference.
 

Parameter Type Hinting

PHP 7
function add(int $a, int $b) : int {
    return $a + $b;
}

function greet(?string $name) : void {
    if (isset($name))
      print('Hello ' . $name); 
    else
      print('Hello');
}


$val = add(2, 3);     // 5
$val = add(2, "sam"); // ERROR

greet(null);    // Hello
greet("Sam");   // Hello Sam
greet([]);      // ERROR

Type Declaration also known as Type Hints in PHP allows functions to require that parameters or return value are of a certain type at call time.

To specify a Type Hint, the type name should be added before the parameter name, and for the return type, it should be added before the open curly brace ({).

Types are:

  • array
  • callable
  • bool
  • float
  • int
  • string
  • self
  • iterable
  • object
  • Class/Interface name

Most of the Type Hints are introduced in PHP 7.0.

You can specify the return type void to specify that the function won't return any value. (PHP 7.1)

You can specify an argument or return type can be null using question mark (?) before the Type Hint.

To learn more about this check php.net manual.