Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Anonymous Function PHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 112
    Comment on it

    Welcome to Findnerd. PHP introducing new concepts version by version but many of us still are using old style of coding. We need to study these new concept and implement these concepts in our code. Today we are going to discuss the anonymous function which is not so new but we do not use it often. Anonymous function is just like a regular function but only difference is that it does not have name. It is without any name. You need to add the semi-colon in the end. Please have a look.

    <?php
    function($emp_name,$salary){
    return ($emp_name . ' taking ' . $salary);
    };
    ?>
    

    You can assign the anonymous function to a variable and can use it later.

    <?php
    
    $getSalary = function($name,$salary){
    return ($name . '' . $salary);
    };
    echo $getSalary();
    ?>
    

    You can call the function with the help of assigned variable. You can also create multiple functions inside array.

    <?php
    $empData = array();
    $empData['salary'] = 
    function($salary){
    return $salary;
    };
    $empData['position'] = function($position){
    return $position;
    };
    echo $empData['position'](344) . '<br />';
    echo $empData['salary'](33344);
    ?> 
    

    We created array named $empData and assigned two anonymous functions to it.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: