Using Arrays of Lambda Functions : Dynamic Function « Functions « PHP






Using Arrays of Lambda Functions

 
<?php
$divisible = array(
    1 => create_function('$n',
        'echo "<p>$n is divisible by 1</p>";'),
    2 => create_function('$n',
        'if ($n % 2 == 0) {
            echo "<p>$n is divisible by 2.</p>"; }'),
    3 => create_function('$n',
        'if ($n % 3 == 0) {
            echo "<p>$n is divisible by 3.</p>"; }'),
    4 => create_function('$n',
        'if ($n % 4 == 0) {
            echo "<p>$n is divisible by 4.</p>"; }'),
    5 => create_function('$n',
        'if ($n % 5 == 0) { echo "<p>$n is divisible by 5.</p>"; }'),
    );

foreach($divisible as $func) {
    $func(2000);
}
?>
  
  








Related examples in the same category

1.Calling Functions Dynamically
2.Calling a Function Dynamically
3.Calling Variable Functions
4.Calling a Function Dynamically
5.Creating Dynamic Functions
6.return the function name from a function call or calculation
7.Variable function example
8.Variable Function Names
9.Using a variable function determined by some input variable
10.String Manipulation