PHP Functions

Last Updated : May 25, 2021

PHP Functions are code blocks that can be called multiple times to perform a certain task. PHP Functions will not be executed by default once we created them. Instead, they will be executed when we call their name in the program. Functions may return a value, and it may take some parameters to process depending upon the scope.

In PHP, there are a bunch of in-built functions are available to do various operations like strlen(), array_merge(), fopen(), etc.,

How to create Functions in PHP

In PHP, we have many built-in functions available to use; however, PHP allows us to create custom User-defined functions to do certain operations. PHP Functions are created or declared using function keyword and open/close parentheses() at the end of the user-defined name and, then, with open/close curly braces{}. Open curly brace { considered as the start of the function and close curly brace } considered as close to the functions.

Syntax:

function function_name(argument1, argument2, ...) {
   //code to be executed
   return value;
}

Naming Conventions of User-Defined Functions in PHP

PHP has few rules for User-defined function naming conventions; it’s almost the same as the Variable naming conventions we already learned in the previous chapter. Please find below the major rules,

  • A function’s name should start with alphabets(a-z, A-Z) and underscore(_).
  • A function’s name should not start with numbers or special characters.
  • A function’s name should not have space anywhere in between the names.
  • A funciton’s names are case-insensitive; you can use regardless of capital/small letters in them

Example of User-Defined Functions in PHP

User-defined functions are created by giving any name relevant to the operation it will do and with required parameters, if needed. Also, once functions are created, they can be called by their name, followed by open/close parentheses. Please find below the examples,

<?php
//user-defined function
function greetings() {
  echo "Welcome to our PHP Tutorial.";
}
echo 'John, ', greetings();
echo '<br>';
echo 'Peter, ', greetings();
?>

Scope of a variable in PHP Functions

In PHP Functions, whatever the variable we create inside the function has local scope by default, which means it will expire outside the function definition. We already learned in detail about the Scope of a Variable in the previous chapter. In Functions, we can use Local scope, Global scope variables along with Function parameters scope variables.

How to return a value from a Function in PHP

In PHP, to return a value from the function, we have to use a return statement. The PHP return statement is used to return a value from a function and terminate the code execution process after a return. A typical function can have a return value, but when we included a return statement without any value, it will return a NULL value. Please find below the example,

<?php
//function with return a value
function greetings() {
  return "Welcome to our PHP Tutorial";
}
echo greetings().', John';
echo '<br>';
echo greetings().', Dan';

//function without return a value
function greetings() {
  echo "Welcome to our PHP Tutorial.";
  return;
}
echo 'John, ', greetings();
echo '<br>';
echo 'Peter, ', greetings();
var_dump(greetings());
?>

Function with Arguments or Parameters in PHP

In PHP, functions capable of receiving the values and processing them, these values are called arguments or parameters. These arguments are separate by comma,, and we can specify n number of arguments in the function. These arguments are variables and have the same variable’s rules to define them. Arguments should pass inside open/close parentheses after a function name.

PHP Function’s arguments are passed by few categories like Passing arguments by Value, Passing arguments by Reference, and Default arguments with values. We already learned an example about passing arguments by value/reference. Please check this chapter to get more information. Please find below the examples,

<?php
// with arguments
function addition($num_1, $num_2) {
  $num_3 = $num_1 + $num_2;
  return $num_3;
}
echo addition(20, 22) .'<br>';

// with default argument values
function subtraction($num_1, $num_2 = 5) {
  $num_3 = $num_1 - $num_2;
  return $num_3;
}
echo subtraction(20, 10) .'<br>';
echo subtraction(20) .'<br>';
echo subtraction(220, 110) .'<br>';
?>

Type Casting the Arguments in PHP Functions

We learned about the Type Casting for variables in detail in the previous chapter of this tutorial series; we can do a similar typecasting for Function’s arguments because these arguments are considered variables so that the same rules will be applicable.

To cast any data type for the arguments in a function, we must specify the data type in front of the argument. If we pass the wrong data type value when we call the function, PHP will return an error. Please find below the examples,

<?php
function multiply(int $a, float $b) {
  return $a * $b;
}
echo multiply(4, 4.5) .'<br>';
echo multiply(4, 5) .'<br>';
echo multiply(4.5, 4) .'<br>';
?>

Function Types in PHP

In PHP, we can create a function for different purposes, and it can be Variable/Dynamic Functions, Callback Functions, Recursive Functions, etc. Each function type is used to perform different operations depending upon the scenarios. We will learn about them in detail below.

Variable/Dynamic Functions in PHP

We can create many functions for various tasks and using the dynamic variable name, we can call them when required. The callable function name doesn’t have to be the same as the function name since we use it dynamically by checking some conditions or without it. Please find below examples,

<?php
function addition($a, $b) {
  return $a + $b;
}
function subtraction($a, $b) {
  return $a - $b;
}
$add_function = 'addition';
echo $add_function(3, 5) .'<br>';

$sub_function = 'subtraction';
echo $sub_function(30, 5) .'<br>';
?>

Callback Functions in PHP

In PHP, we can create a function, and it can be called on-demand dynamically using a built-in function called call_user_func. This built-in function invokes the function mentioned as an argument; it will take the user-defined function as an argument for processing. Please find below the example,

<?php
//callback function
function add($a, $b) {
  echo ($a * $a) + ($b * $b);
}
call_user_func('add');
?>

Recursive Functions in PHP

In PHP, we can create a function that can call itself for processing until certain conditions are satisfied or until some period. The function called itself, again and again, is called the Recursive Function. Please find below the example,

<?php
//factorial example using recursive function
function find_factorial($num) {
  if ($num == 0) return 1;
  return $num * find_factorial($num - 1);
}
echo 'Factorial of 5 is: ', find_factorial(5);
?>

In this tutorial we learned about PHP Functions and its functionalities. Functions are very important concepts in any language, please learn them well to create better programming. Let’s move into the next chapter of this tutorial.