phparray-pushucfirstzend-certification

array_push - befuddled with Zend test example


While studying for Zend test, I ran across this code which works, but I cannot figure out why given the two strange props in array_push. strtolower and ucfirst are used where there should be variables. Have I missed some documentation?

<?php
    $str = 'MY STRING';

    $funcs = array();

    array_push($funcs, 'strtolower', 'ucfirst');

    foreach ($funcs as $func) {
        $str = $func($str);
    }

    if ($str == 'My string') {
        echo "Correct";
    }
    else {
        echo "Incorrect";
    }
?>

Solution

  • PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth.

    Source: http://php.net/manual/en/functions.variable-functions.php