PHP Functions

A PHP function is a piece of code that can be used repeatedly in a program. The main advantage of using functions is that they are reusable; if you have a code that needs to be performed a number of times, a function is the best idea for this solution.

They can be either defined by you or by PHP (and as we know that PHP has a rich collection of built-in functions). And PHP Function names are case-insensitive.

Key Benefit Of Using PHP Functions

  • Increase Code Reusability
  • Cut down extra Code
  • Easy to Use and understand



  • Create a User Defined Function in PHP

    The basic syntax of creating a User Defined function can be give with:

    Syntax

    Let’s see how to create a function in PHP given below:

    <?php
    function function_Name(){
        // Code to be executed
    }
    ?>
    

    It is very easy to create user-defined function start with the word function, followed by the name of the function you want to create followed by parentheses i.e. () and you will put your function’s code inside the curly brackets {}.

    Let’s see a given below simple example of an user-defined function:

    Example

    <?php
    function firstfun() {
        echo "Welcome to Netzole!";  // code execute here
    }
    
    firstfun(); // call the function
    ?>
    

    So here we can say we create a function which is names firstfun() and here we can see opening curly brace ( { ) indicates the starting of the function code, and the closing curly brace ( } ) indicates the end of the function. The function output will be Welcome to Netzole!.

    Output:

    Welcome to Netzole!




    PHP Functions with Parameters

    You can specify parameters when you define your function to accept input values at run time. The parameters work like placeholder variables within a function; they’re replaced at run time by the values (known as argument) provided to the function at the time of invocation.

    Syntax

    function myFunc($oneParameter, $anotherParameter){
        // Code to be executed
    }
    

    You can define as many parameters as you like. However, for each parameter you specify, a corresponding argument needs to be passed to the function when it is called.

    The getSum() function in following example takes two integer values as arguments, simply add them together and then display the result in the browser.

    Example

    <?php
    // Defining function
    function getSum($num1, $num2){
      $sum = $num1 + $num2;
      echo "Sum of the two numbers $num1 and $num2 is : $sum";
    }
     
    // Calling function
    getSum(20, 30);
    ?>
    

    Output:

    Sum of the two numbers 20 and 30 is : 50




    PHP Function Call By Value

    In PHP call by value, actual value is not changed if it is changed inside the function.
    Let’s see the example for proper understand the concept of call by value

    Example

    In this example, variable $str is passed to the adder function where it is concatenated with ‘Call By Value’ string. But, printing $str variable results ‘Hello’ only. It is because changes are done in the local variable $str2 only. It doesn’t reflect to $str variable.

    Example

    <?php  
    function adder($str2)  
    {  
        $str2 .= 'Call By Value';  
    }  
    $str = 'Hello ';  
    adder($str);  
    echo $str;  
    ?>  
    

    Output:

    Hello




    PHP Function Call By Reference

    In PHP call by value, actual value is changed if it is changed inside the function. In such case, you need to use & (ampersand) symbol with formal arguments. The & represents reference of the variable.

    Let’s see the example for proper understand the concept of call by value.

    Example

    In this example, variable $str is passed to the adder function where it is concatenated with ‘Call By Reference’ string. Here, printing $str variable results ‘This is Call By Reference’. It is because changes are done in the actual variable $str.

    Example

    <?php  
    function adder(&$str2)  
    {  
        $str2 .= 'Call By Reference';  
    }  
    $str = 'This is ';  
    adder($str);  
    echo $str;  
    ?>  
    

    Output:

    This is Call By Reference




    PHP Recursive Function

Pin It on Pinterest

Shares
Share This