Variable Function Names : Dynamic Function « Functions « PHP






Variable Function Names

 
<?php
function is_divisible_by_1($num) {
    echo "<p>Of course $num is divisible by 1, everything is!</p>";
}

function is_divisible_by_2($num) {
    if ($num % 2 == 0) {
        echo "<p>$num is divisible by 2.  That means it is even.</p>";
    }
}

function is_divisible_by_3($num) {
    if ($num % 3 == 0) {
        echo "<p>$num is divisible by 3.</p>";
    }
}

function is_divisible_by_4($num) {
    if ($num % 4 == 0) {
        echo "<p>$num is divisible by 4.  It is double-even!</p>";
    }
}

function is_divisible_by_5($num) {
    if ($num % 5 == 0) {
        echo "<p>$num is divisible by 5.  It ends in a 0 or a 5.</p>";
    }
}

for ($i = 5; $i > 0; $i--) {
    $var = 'is_divisible_by_' . $i;
    $var(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.Using a variable function determined by some input variable
9.String Manipulation
10.Using Arrays of Lambda Functions