1    function foo($i){
2       return bar($i)*4;
3       function bar($i){
4           return $i*4;
5           }
6       }
7    echo foo(4);
return
Fatal error: Call to undefined function bar() in /var/www/index.php on line 2
why doesn't it work? it works well in javascript, while it works when i do this:
function foo($i){
   return bar($i)*4;
   }
function bar($i){
   return $i*4;
   }
                Define the function above your return value, otherwise it never gets executed.
<?php
function foo($i){
    function bar($i){
        return $i*4;
    }
    return bar($i)*4;
}
echo foo(4);
?>
                        It doesn't work as you are calling bar() before it has been created. See example 2 here:- http://www.php.net/manual/en/functions.user-defined.php
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With