What is a function in PHP and why we use it?

A function in PHP is a self-contained block of code that use to performs a specific task in the program. In another word, we can say that PHP function is a piece of code that can be reused many times in a PHP based web application.

There are two types of function. One is the built-in or internal functions that already in PHP and you can use it. The other one is user defined function that can be defined by users for a task.

Some of the built-in functions are time(), strllen(), unlink(), disk_free_space(), fclose() etc. There is a huge amount of built in functions available in PHP which can be used in many purposes.

Now I am going to tell you how to define user define function and how to use it for a task.

In the code below you can see a simple PHP function:

 function myFunction() {
        // Your code inside PHP function
        echo "This is from PHP function";

We have created a function and name it “myFunction”. So what will the above function do?

Well, the function will not perform any task until we call it. Below is the code which will call the above PHP function:

// Call the function

It will simply display “This is from PHP function” on the web page. So you just need to call the function with the function name and then closing brackets.

Another amazing useful thing is that a function can take arguments or parameter just like below:

function functionName($var) {
     //  Your code goes here

Adding two numbers using PHP function

Now here I am going to show you an example so that you will understand PHP function with more easily.

Below is a  function that we have created to add two numbers:

function addNum($num1, $num2)
	$sum = $num1 + $num2;
        //return the sum result of two numbers
	return $sum;

The above function will return the sum of two numbers. We are going to provide these two numbers as arguments inside the function. So we have given the function name with two variable like this:

addNum($num1, $num2)

So how to use the above function so that we will get the sum result of two numbers. below is the given code that will display the summation of those two numbers:

$sumResult addNum(8, 6);
echo $sumresult;

The code will display “14” on the web page as the sum of 8 and 6 is 14. You can also do it in one line like you can see it below:

echo addNum(8, 6);

Why we use PHP function? What is the advantage of using the function?

So why we should use the function in PHP?

Well, the main purpose of using functions in PHP projects is to decrease the amount of code and to increase the reusability of code. Below is the given advantages of using PHP function:

Functions reduce the repetition of code in a program:

You can use a PHP function multiple time as you like without writing the code again and you do not have to create another function for the same task. Thus, functions help to reduce the amount of code. It helps to save time.

Functions increase the reusability of code:

Because a function is separated from the rest of the script, it’s so easy to reuse a function in other applications just by including the PHP file containing those functions and then call it.

Functions make it easier to maintain code:

As functions can be called multiple time without writing code again, it is easier to make changes once inside the function and all the changes will be available wherever you use the function. So it is easy to maintain all your codes.

Functions make it easy to detect and then eliminate the errors:

Whenever some error occurs, you will be informed from which functions exactly the error coming from and for this reason it is much easier to find the error and to eliminate it.

So I hope you have to understand how to create a function in PHP and what is its usage. Now try to create your own function. taking some time in practicing will make you increase your programming skill.

Leave a Reply

Your email address will not be published. Required fields are marked *