PHP Functions

Function is a block of code which when executed performs specific task. The main reason for using function is code reuse, write once execute any time. There are times when you need to write the same set of code more than once in your program in such case we can use function. You can create a function once and whenever you need it all you need is a function call. There are two main types of functions used in PHP.

  1. User defined function
  2. Inbuilt function

In this tutorial we will be discussing the first type that is user defined function later we will cover some of the important inbuilt functions used in PHP.

User Defined Function

The function created by user is known as user defined function. By user defined we mean to say the definition of function is written by the user.

Advantages

  • It facilitates top down modular programming
  • The length of program is reduced
  • Write once use any time

There are mainly two important things you need for working with user defined function

  1. Function Definition
  2. Function Call

How to define a User Defined Function

Syntax

function function_name([parameters])
{
       Statements to be executed;
       [return value;]
}

Parameters are the inputs to be given to your function which is optional, you can pass as many arguments you want or you can pass none. The return value specifies the output which your function will produce when called, it’s also optional.

The naming convention of function is similar to that of variable that is it should start with either underscore (_) or with letter, it should not contain any special characters and it should not be any keyword.

Function Call

Given is the syntax for calling a function.

function_name([Parameters if Any]);

Whenever you want to execute any defined function you need to call the same function along with required parameters and if the function returns any value then in that case you need to store the output. Let’s look at one simple example.

For Ex.

<?php
        function Display()//function Definition
        {
                  echo "Hello DiscussDesk<br>";
        }
        Display();//function call
        Display();
?>

Here as you can see from the example first we have defined a function named Display() which takes no parameter and it has no return value as well. Later in our program we have called Display() function twice which will execute the function twice.

Output

Hello DiscussDesk
Hello DiscussDesk

Function With Parameters

Now Let’s take one example in which we have a function which takes some parameters , parameters or arguments are the inputs which we should give to our function when we try to call the function.

For Ex.

<?php
       function sum($a,$b)
       {
                 $c=$a+$b;
                 echo $a." + ".$b." = ".$c."<br>";
       }
       sum(10,20);
       sum(10.30,20.30);
       //sum("Pratik","Parmar");
?>

Output

10 + 20 = 30
10.30 + 20.30 = 30.6

If you have noticed we need not to specify the parameter type which we have seen in some other languages like c and c++ , PHP is a loosely typed language so it frees you from specifying the data types it will automatically set the data type on basis of the parameters you give when you call your function. But you should be careful while calling functions. In our example we have used string parameters for calling sum() function which will result in some invalid output(the code for the same is given in comment)

Function returning value

To return a value we need to write a return statement at the end of function body. The example for the same is given below.

For Ex.

<?php
       function sum($a,$b)
       {
                 $c=$a+$b;
                 return $c;
       }
       echo "10 + 20 =".sum(10,20)."<br>";
       echo "10.30 + 20.30 =".sum(10.30,20.30)."<br>";
?>

Output

10 + 20 = 30
10.30 + 20.30 = 30.6

Function with Default parameter value

You can give default value to a parameter when you are defining any function. When calling the function if you don’t pass the parameter default values will be considered, if you give parameter it will overwrite the default one. Let’s understand it with one example.

For Ex.

<?php
       function Display($a="DiscussDesk")
       {
               echo "Hello ".$a."<br>";
       }
       Display();    //no parameter given default value considered
       Display("Pratik");    //parameter given default value overwritten
?>

Output

Hello DiscussDesk
Hello Pratik

As you can see in first call we have not passed any argument in that case default argument is considered that’s why Display() prints Hello Discuss Desk. In second call we have passed one argument which overwrites the value of default argument so the function prints Hello Pratik.

So to conclude in this tutorial we have learnt what is function? And how to use user defined function in PHP.

Share this Article on Social Media

Get Updates, Scripts & Other Tutorials to Directly to your Email

Over 20000+ Happy Readers already subscribed. (We don't send spam email). Every email subscriber can get our latest updates and download our 100+ scripts.

Comments