Menu Close

Articles

PHP: Functions

A function in any programming language is a relation between one or more parameter values and a result value. This term comes from mathematics. Usually a function is a named block of code enclosed in curly brackets: {…}.

User Defined Functions

These are functions that you can define in source code specific to your application. You must be creative when you design a function, to give it a proper name. Usually functions perform an action so they must be verbs or combination of words separated by underscore. The name is case insensitive, but usually you write the name with lowercase characters.

Example:

Notes:

  • Parameters can be optional if they are declared with initial value,
  • Function call can use variables, expressions or constants to bind values for each parameter,
  • Parameters can be assigned by position or by name using $name = value pairs,
  • A function can return a single result but it can have output parameters.

Output Parameters

Functions can receive formal parameters by reference. This is explicit in PHP using additional prefix “&” in front of parameter name. This will enable you to modify parameter value and this way you can create functions that return multiple values.

Example:

Conditional Declaration

PHP is a dynamic language, so it can do weird things you do not expect from a regular computer language. You can decide to create a function or not depending on a particular condition. If the function exist you can call it, otherwise you do not.

Example:

Local functions

A function can be defined inside another function. This is called “local function” and does not exist outside of the parent function. Unfortunately local functions do not have access to outer variables but can call other local functions and can access global variables.

Example:

Recursive Functions

A recursive function is a function that call itself with different parameters. It is very easy to create a wrong program this way that will crash PHP engine. Any recursive function can be replaced by a loop and a stack. So, avoid deep recursive functions as much as possible.

Example:

Variable arguments

A function can accept a variable list of arguments.  All arguments are captured into an array. To declare this array you can use “…” in front of array parameter like in C. Then you can use this array to read all values and use them to produce a result. 

Example:

Using type hint

PHP is dynamic language, however the code becomes fragile if the type is not enforced. Function parameters can be declared with type. Then if you call the function with the wrong type you get an error. To do that you can use following type hints:

class nameThe parameter must be an instanceof the given class name or interface name.
selfThe parameter must be an instanceof the same class as the one the method is defined on. This can only be used on class and instance methods.
arrayThe parameter must be an array.
callableThe parameter must be a valid callable.
boolThe parameter must be a Boolean value: true or false.
floatThe parameter must be a floating point number.
intThe parameter must be an integer.
stringThe parameter must be a string.
iterableThe parameter must be either an array or an instanceof : Traversable.
objectThe parameter must be an object.

Example:

This example will fail. It is because the call for sum is using the wrong types!

Output:

PHP Fatal error: Uncaught TypeError: Argument 1 passed to sum() must be of the type integer, float given, called in index.php on line 9 and defined in index.php:4 Stack trace: #0 index.php(9): sum(1.5, 2.5) #1 {main} thrown in index.php on line 4

Note: If you do not enforce types, the automatic conversion will convert the wrong types to good types:

Return Type

As you can do for parameters, you can declare a result type for functions. If the function do not return the correct type it can trigger an error. The result type is declared after the function parameters using “:”, then function block {…}.

Example:

Following example will fail, since the call will force incorrect result type.

Null return

A function that can return null, need a special notation for return type using prefix “?”.

Example:

Variable function

You can store function name into a variable. Then you can use variable to call function by name. 

Example:

Internal functions

PHP comes with many functions and constructs. There are also functions that require specific PHP extensions compiled in, otherwise fatal “undefined function” errors will appear. There are many core functions that are included in every version of PHP, such as: string and variable functions.

Next article: Arrays