1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

PHP Functions

Discussion in 'PHP' started by MinalS, Nov 16, 2015.

  1. MinalS

    MinalS New Member

    Joined:
    Jul 8, 2014
    Messages:
    138
    Likes Received:
    30
    Trophy Points:
    0
    The use of function is similar to other language functions, take one or more parameters as input, process and provide the corresponding result. The block of code to be used more frequently is added inside the functions. The execution of function is dependent on two points as create the function and call the function.

    PHP function creation



    The function keyword is used for the function creation. The code is added inside the curly braces.

    Syntax of function creation

    Code:
    function FunctionName()
    {
        Code to be executed;
    }
    
    User can start the function name using a letter or underscore. A number cannot be used for function declaration.

    Example:

    Code:
    <html>
        <body>
            <?php
                /* function definition*/
                function display(){
                    echo "Welcome User";
                }
                /*Call the function */
                display();
            ?>
        </body>
    </html>
    

    Function with parameters



    User can pass the parameters to the PHP functions. The parameters are used as variables. They are defined following the function name, places inside the parentheses. More than one parameter can be added by using comma.

    Code:
    <html>
        <body>
            <?php
                function multiplyno ( $no1, $no2)
                {
                    $result = $no1 * $no2;
                    echo "Multiplication of two numbers is : $result";
                }
                multiplyno ( 5, 10 );
            ?>
        </body>
    </html>
    
    If we don't need to pass the variable values each time a function is called, we can use default values for parameters. The default parameters are the last parameters.

    Code:
    <html>
        <body>
            <?php
                function multiplyno ( $no1, $no2 = 2)
                {
                    $result = $no1 * $no2;
                    echo "Multiplication of two numbers is : $result";
                }
                multiplyno ( 5, 10 );
                multiplyno ( 5 ); // Takes second argument as 2
            ?>
        </body>
    </html>
    

    Passing with reference



    User can pass the values as reference to the functions. The variables are referred using the ampersand sign before the variable name. The modifications to the argument are reflected to the value of the variable.

    Example:

    Code:
    <html>
        <body>
            <?php
                function increaseno ( &$no )
                {
                    $no + = 1;
                }
                $num = 2;
                increaseno ( $num );
                echo "The number is $num";
            ?>
        </body>
    </html>
    

    Function returning values



    In PHP, the return keyword is used for returning the value from the functions. More than one value can be returned from a function.

    Example:

    Code:
    <html>
        <body>
            <?php
                function multiply( $no1, $no2)
                {
                    $result = $no1 * $no2;
                    return $result;
                }
                $output = multiply ( 2, 5 );
                echo "The value after computing the function is: $output";
            ?>
        </body>
    </html>
    
    

    Dynamic function calls



    User can use the function names as the strings. The variables on which it is assigned are used as function name.

    Example:

    Code:
    <html>
        <body>
            <?php
                function UserWelcome()
                {
                    echo "Welcome<br/>";
                }
                $function_id = "UserWelcome";
                $function_id();
            ?>
        </body>
    </html>
    
     

Share This Page