Page 1 of 1

Calling a function inside a function...?

Posted: Fri Mar 03, 2006 6:12 am
by welsh_sponger
Is it possible to call a function inside another function?

For example, i call a function from script1.php...
the function i call processes some calculations on some values, now in this function i call another function to do some more calculations with the new variables...

However i cant get that to work, error message reads "Fatal error: Call to undefined function..." even though i have declared the 2nd function in the first functions script...

The code... (1st function called from another script)

Code: Select all

<?php
include ("function.php");

function conversions($somevariables, $somevariables2, $somevariables3....)
{

      // does some calculations on the $somevariables...


            //and then i want to call the function below passing it the new variables ive just calculated...
            $values = function($somevariables, $somevariables2, $somevariables3....));

}

?>
Is this possible?

Thanks

Posted: Fri Mar 03, 2006 6:32 am
by LiveFree
Yes ~ just as long as the function inside is defined BEFORE the one that is being defined now

Posted: Fri Mar 03, 2006 6:39 am
by shiznatix
i don't think thats true tucker. Yes you can call any function within another function, you can even call the function you are currently in inside of itself (this is called recursion). But php gathers all functions before it executes the script (thats why you can put your functions at the bottom of the script and call them at the top of the script and it will still work). Because of this it would not make sence that a function that you call within another function has to be declared above it in the code.

Posted: Fri Mar 03, 2006 7:25 am
by darryladie
i think shiznatz is right but remember a recursive is calling the same function that is being processed within itself this is not quite the same thing as calling a different function.