Calling a function inside a function...?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
welsh_sponger
Forum Newbie
Posts: 14
Joined: Fri Feb 03, 2006 7:46 am

Calling a function inside a function...?

Post 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
LiveFree
Forum Contributor
Posts: 258
Joined: Tue Dec 06, 2005 5:34 pm
Location: W-Town

Post by LiveFree »

Yes ~ just as long as the function inside is defined BEFORE the one that is being defined now
User avatar
shiznatix
DevNet Master
Posts: 2745
Joined: Tue Dec 28, 2004 5:57 pm
Location: Tallinn, Estonia
Contact:

Post 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.
darryladie
Forum Commoner
Posts: 62
Joined: Thu Mar 02, 2006 6:14 pm
Location: East Sussex, UK

Post 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.
Post Reply