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)