PHP - Functions

    PHP - Functions

    Functions can be defined as the block of the code or some set of instructions that executes by the PHP. these functions may or may not take parameters to pass to that code block. There are two types of functions that are supported by PHP.

    1. Built-in functions

    As we all know that PHP has a vast library and provides you with many built-in functions that have specific code and are stored in PHP. we can call these functions from your existing code so that you do not have to write extra code for that functionality. For example- print_r(), var_dump() and many more. Some may take a parameter and some are called without being passed with parameters.

    2. User-defined functions

    Apart from built-in functions, users can also define their own function as per their requirement which can be called from any code.

    Features of functions

    1. Reusability

    once a program is defined, it can be used anywhere and the number of times. These code blocks can be reused till they last. This will help you to save time and effort of writing code again and again.

    2. Error detection

    Functions break the code within small code sections so errors can be easily detected within small code blocks.

    3. Easy maintenance

    These functions are easy to maintain and take care of all the small code sections very easily. You can create a function that is prefixed with a “function” keyword and has open and close brackets for passing the parameter. You can use alphabets or underscore for the function name but avoid using the number as the first character of the function name.

    Syntax-

    Function function_name($par1, $par2….)
    {
    //code section;
    }

    These functions can contain a number of variables as per user requirements within the parenthesis called parameters. These parameters hold the value during run-time. The values that are passed to these parameters during a function call are called arguments. An argument is a value that will be passed and the parameter will hold that value. You can consider an argument and the parameter to be the same and during a function call, you have to provide value for every argument else you will get an error.

    Example

    <?php
    // function with 2 parameters
    function Func_demo($var1, $var2)
    {
    $prod = $var1 * $var2;
    echo "Result is $prod";
    }
    // Passing arguments during the function call
    Func_demo(2, 3);
    ?>

    Output 6

    Using default value for the function parameter

    You can use a default value for the argument for function parameters. In case, an argument is not passed for a particular parameter then PHP will use a default value that is set for this parameter. In the below example, in the second function call, there is no value provided for the var1 parameter. In that case, PHP will consider the default value 12 that is set in the parameters of the function.

    Example-

    <?php
    // function defined with default parameter
    function func_demo($str, $var1=12)
    {
    echo "$str n";
    }
    // Caling the function with provided values
    func_demo("Ram", 15);
    // no value passed for var1
    func_demo("Jacob");
    ?>

    Output Ram Jacob

    Returning values from the function

    Functions use the return keyword to return the value to the part of the program from where it is called. The function can return any type of data. Once the return statement is encountered within the code block the control will be transferred to the part of the calling function. In the below example, the control will be transferred from the return statement to the calling function and the echo “hello” statement does not execute.

    Example -

    <?php
    function func_demo($var1, $var2)
    {
    $prod = $var1 * $var2 ;
    return $prod; //returning the result
    echo "hello";
    }
    $Value = func_demo(2, 3);
    echo "The result is $Value";
    ?>

    Output The result is 6 You can pass two types of arguments to a function parameter, pass by value and pass by reference. In pass by value, the value will be changed within the function but remain unchanged outside the function. But in the pass by reference, the value will get changed as we are passing the address of the value.

    Example

    <?php
    // pass by value
    function func_val($var) {
    $var += 2;
    return $var;
    }
    // pass by reference
    function func_ref(&$var) {
    $var += 10;
    return $var;
    }
    $num1 = 10;
    func_val($num1);
    echo "ref value $num1 ";
    echo "<br>";
    func_val($num1);
    echo "pass value $num1";
    ?>

    Output

    ref value 10
    pass value 10

    People are also reading: