Functions increase both the readability and reusability of the code. Without using the function, a lot of repetition of code occurs. In this PHP tutorial, you will learn the PHP Functions in detail.

What is PHP Function and How to declare a function in PHP?

PHP functions are its real power. There are two categories of functions in PHP.

  1. Built-in Functions: PHP provides more than 1000 built-in functions. However, These functions are ready to use. You just need to call them by their name. You can go through the PHP built-in functions here.
  2. Custom Functions: You can define your own functions and call them anywhere in the code. In this article, we will talk about the custom functions. Custom functions as also called user defined functions.

User-Defined Functions in PHP

In this section, we will learn how to declare and define our own functions in PHP. Before digging into it, let’s know about some key facts about PHP functions.

  • A function is a block of statements. It can be used repeatedly anywhere in the code.
  • When webpage reloads, the php functions are not called automatically.
  • It is executed when called anywhere.

How to declare a user-defined function in PHP?

To declare a custom PHP function, use the following general syntax.

function functionName() {
  code block related to this function;
}

For instance

<?php
function myFunc() {
  echo "Hello world!";
}
myFunc(); // call tge function
?>

Explanation of the example

  • In the above example, the function is declared using function keyword. After that, round brackets function() indicates the start of the function.
  • Code block of the function starts with { and ends with }.
  • Return statement is required in the function, otherwise it throw error.

Function Arguments

The parameters inside the function round brackets are called function arguments. Functions arguments can b single or multiple. Multiple function arguments are separated by commas.

Note: Function names are not cased sensitive. For instance, function abc() and function ABC() are different.

Function arguments are just like the variables. The arguments are given in the round brackets of the function. There can be one or multiple arguments, separated by a comma. These arguments can be accessed and modified inside the function.

Example of a function with a single argument

<?php
function familyName($fname) {
  echo "$fname Refsnes.<br>";
}

familyName("Jani");
familyName("Hege");
familyName("Stale");
familyName("Kai Jim");
familyName("Borge");
?>

In the above example, the receives one argument and concatenates it with the string. Then this function is called five times, with different argument values. You will see that the function will print the value of the argument along with the string.

Example of a function with two arguments

<?php
function familyName($fname, $year) {
  echo "$fname Refsnes. Born in $year <br>";
}

familyName("Hege", "1975");
familyName("Stale", "1978");
familyName("Kai Jim", "1983");
?>

In the above example, the function receives two arguments, separated by a comma. The calling method is the same as described in the above example. However, we need to pass two values to the function while calling it.

What if you pass insufficient arguments to a function?

If you try to call a function with insufficient arguments, it will give an error. However, there is always some way to avoid this. It is described below.

How to use the default value in functions argument – PHP

A function can assign a default value to the argument. It allows calling the function without passing that argument. It is shown below,

<?php declare(strict_types=1); // strict requirement
function setHeight(int $minheight = 50) {
  echo "The height is : $minheight <br>";
}

setHeight(350);
setHeight(); // will use the default value of 50
setHeight(135);
setHeight(80);
?>

In the above example, we have called the function without the second parameter. The value of the second argument will be taken from the default value that is 50.

Passing PHP functions argument by reference

In the above section, we have learned about passing the variables by reference. If a variable is used without the reference, you will not be able to modify the value inside the function. Because, in this case, the OS creates two copies of the argument. Changing the value of argument inside the function on change the one copy. On the other hand. if a variable is passed by reference (using & sign), it does not create multiple copies. So, changing the value inside the functions will change the value of the original argument.

Return from PHP functions

Return statement stops execution of the function and returns the value.

For instance

<?php declare(strict_types=1); // strict requirement
function sum(int $x, int $y) {
  $z = $x + $y;
  return $z;
}

echo "5 + 10 = " . sum(5, 10) . "<br>";
echo "7 + 13 = " . sum(7, 13) . "<br>";
echo "2 + 4 = " . sum(2, 4);
?>

In the above example, there are two arguments for this function. They are of the integer type. Their sum is returned. The function is called with different values of arguments are the return value is observed.

The type of the function and the return value must be of the same type. For instance.

<?php declare(strict_types=1); // strict requirement
function addNumbers(float $a, float $b) : float {
  return $a + $b;
}
echo addNumbers(1.2, 5.2);
?>

declare(strict_types=1) in php

In PHP, write the above line on the top of the PHP file. It makes the function arguments type strictly follow. It helps to avoid any invalid statements, such as a sum of a number and string number. For instance.

<?php
function addNumbers(int $a, int $b) {
  return $a + $b;
}
echo addNumbers(5, "5 days");
// since strict is NOT enabled "5 days" is changed to int(5), and it will return 10
?>

For PHP 8 official reference to functions, click here.