PHP Functions

A function is a reusable piece of code that can take in arguments and also return a result after performing a specific task.

A function can be called inside other functions.

A function will only execute when its called.

There are two types of functions in PHP which are PHP built-in functions and PHP user defined functions.

PHP Built-in Functions

PHP has a vast collection of built-in functions that perform a specific task.

These functions can be called directly without being defined by the user.

Be sure to check out PHP documentation for a complete list of useful internal or built-in functions.

PHP User Defined Functions

PHP lets us define our own functions but with a few things in mind:

  • A function must always start with the function keyword
  • A function must be called with its name followed by opening and closing parenthesis and its arguments in the parenthesis separated by a comma if it has any.
  • A function is not case-sensitive.
  • A function should not start with a number. It can either start with an alphabet or underscore.

PHP Functions Example

  Do It Yourself

In the above example, we defined a function printHello that outputs "Hello World!".

PHP Function Arguments

A function argument is the value that is passed to a function when it's called. During a function definition, a function needs to specify a variable that assigns to its corresponding argument. The name passed to a function during its definition is called a parameter.

function name($firstParameter, $secondParameter){

// code to be executed

}

name("firstValue", 10);

The above syntax is a function that has two parameters $firstParameter and $secondParameter which assigns to its corresponding arguments first value and 10.

PHP Function Arguments Example

  Do It Yourself

The above example is a function that takes in two numbers as arguments and outputs its sum.

Most programming languages including PHP, lets you assign default argument values to our function parameters. If no value is passed to the parameter, the default argument value will be used.

Note that it is impossible to assign a default argument value before another non-default argument value, this will lead to an error. Default argument value should be on the right side of any non-default argument value.

PHP Default Function Arguments Example

  Do It Yourself

Returning Values From PHP Functions

PHP functions lets you return a value of any type(objects, integers, arrays, string...etc) by using the return statement.

PHP Functions - Returning Values Example

 $numTwo) {
                     return $numOne;
              } else {
                    return $numTwo;
             }  
        }

        echo getMax(0,2); //outputs: 2


?>
  Do It Yourself

The above example is a function that has two parameters $numOne and $numTwo and returns the maximum value among the two.

Passing Arguments To A PHP Function By Reference

There are two ways to pass arguments to a function in PHP: by value and by reference. By default, arguments are passed by value which means that if a variable is passed (as an argument) by value to a function, its value only changes inside the function and it does not affect the value of the variable outside the function. But if an argument is passed by reference the value of the variable on the outside can be changed.

Passing arguments by reference to a function requires you to prepend an ampersand sign (&) to the parameter of the function.

PHP Functions - Passing Arguments By Reference Example

  Do It Yourself

The above example shows the difference between passing an argument by value or by reference.

What You Should Know at The End Of This Lesson

  • You should know how to define a function.
  • You should know the difference between a function parameter and an argument.
  • You should know how to return a value from PHP functions.
  • You should know to pass arguments either by value or by reference.