A Function is nothing but a 'block of statements' which generally performs a specific task and can be used repeatedly in our program. This 'block of statements' is also given a name so that whenever we want to use it in our program/script, we can call it by its name.
In PHP there are thousands of built-in functions which we can directly use in our program/script.
PHP also supports user defined functions, where we can define our own functions.
A function doesn't execute when its defined, it executed when it is called.
Let's understand how we can define our own functions in our program and use those functions.
Syntax:
<?php
function function_name()
{
// function code statements
}
?>
{
after the function name marks the start of the function code, and the closing curly brace }
marks the end of function code.Let's write a very simple function which will display a simple "Merry Christmas and a Very Happy New Year" message. This script can actually be very useful when you have to send festive emails to every friend of yours and you have to write the same message in all of them.
<?php
// defining the function
function greetings()
{
echo "Merry Christmas and a Very Happy New Year";
}
echo "Hey Martha <br/>";
// calling the function
greetings();
// next line
echo "<br/>";
echo "Hey Jon <br/>";
// calling the function again
greetings();
?>
Hey Martha Merry Christmas and a Very Happy New Year Hey Jon Merry Christmas and a Very Happy New Year
As we have already seen a simple example of using a function above, you must have understood how time-saving it can be for large programs. Here are a few advantages of using functions for you:
We can even pass data to a function, which can be used inside the function block. This is done using arguments. An argument is nothing but a variable.
Arguments are specified after the function name, in parentheses, separated by comma. When we define a function, we must define the number of arguments it will accept and only that much arguments can be passed while calling the function.
Syntax:
<?php
/*
we can have as many arguments as we
want to have in a function
*/
function function_name(argument1, argument2)
{
// function code statements
}
?>
Let's take a simple example:
<?php
// defining the function with argument
function greetings($festival)
{
echo "Wish you a very Happy $festival";
}
echo "Hey Jai <br/>";
// calling the function
greetings("Diwali");
// next line
echo "<br/>";
echo "Hey Jon <br/>";
// calling the function again
greetings("New Year");
?>
Hey Jai Wish you a very Happy Diwali Hey Jon Wish you a very Happy New Year
As you can see in the example above, how we changed our greetings()
function to start taking arguments, and now it can be used for sending greetings for different festivals.
Sometimes function arguments play an important role in the function code execution. In such cases, if a user forgets to provide the argument while calling the function, it might lead to some error.
To avoid such errors, we can provide a default value for the arguments which is used when no value is provided for the argument when the function is called.
Let's take an example.
<?php
// defining the function with default argument
function greetings($festival = "Life")
{
echo "Wish you a very Happy $festival";
}
echo "Hey Jai <br/>";
// calling the function with an argument
greetings("Diwali");
// next line
echo "<br/>";
echo "Hey Jon <br/>";
// and without an argument
greetings();
?>
Hey Jai Wish you a very Happy Diwali Hey Jon Wish you a very Happy Life
So when you forget to provide an argument while calling the function, to cover up, you can set default values to the arguments to your functions.
Yes, functions can even return results. When we have functions which are defined to perform some mathematical operation etc, we would want to output the result of the operation, so we return the result.
return
statement is used to return any variable or value from a function in PHP.
Let's see an example.
<?php
function add($a, $b)
{
$sum = $a + $b;
// returning the result
return $sum;
}
echo "5 + 10 = " . add(5, 10) . "
";
?>
5 + 10 = 15
Function overloading allows you to have multiple different variants of one function, differentiated by the number and type of arguments they take.
For example, we defined the function add()
which takes two arguments, and return the sum of those two. What if we wish to provide support for adding 3 numbers.
To tackle such situations, what we can do is, we can define two different variants of the function add()
, one which takes in 2 arguments and another which accepts 3 arguments. This is called Function Overloading.
Let's take an example,
<?php
// add function with 2 arguments
function add($a, $b)
{
$sum = $a + $b;
// returning the result
return $sum;
}
// overloaded add function with 3 arguments
function add($a, $b, $c)
{
$sum = $a + $b + $c;
// returning the result
return $sum;
}
// calling add with 2 arguments
echo "5 + 10 = " . add(5, 10) . "<br/>";
// calling add with 3 arguments
echo "5 + 10 + 15 = " .add(5, 10, 15) . "<br/>";
?>
5 + 10 = 15 5 + 10 + 15 = 30
Unfortunately, PHP does not support Function Overloading.
This was just to give you an idea about what function overloading is. In PHP, function signatures are only based on their names and do not include the argument lists, hence we cannot have two functions with same name.