Jump to content

Recursion in PHP


j.smith1981
 Share

Recommended Posts

Hi there,

 

I don't know if this is the correct part of the forum but I am wanting to figure out something about recursion, kind of questioning someone elses code.

 

It's just a general rule though so there's no real need to display it I don't think.

 

However when I say I want to get a function to call another function, would I need to declare (write) the 2nd function within the function that called it?

 

I just am questioning what I saw in another book whereby what I create does not work on my own basis but does when someone else shows an example in a book, where the author created a function that called a function that was written within another, is this legal syntax though is my main question?

 

Kind of looked somewhat like this (very basic and no actual commands or instructions here):

<?php
function myfunction()   {

//  if whatever condition is true, call the second function just below me:
  function mysecondfunction() {

  }
}

Does that make any sense to you?

 

Or would I just go off the book I am reading now where the 2 of them are completely seperated and the first function calls the 2nd, but not actually putting a function within another?

 

Any advice is much helpful,

Jez.

Link to comment
Share on other sites

You can't have a function within a function. You're most likely thinking of classes:

 

class Foo
{
    function bar()
    {
        // ...
    }
}

 

These are a very different concept, far beyond the scope of this post. If you would like to learn more though there's plenty of information in the manual, and an Object Orientated PHP Tutorial on the main site here.

 

Recursion is something completely different, it's basically a function calling itself. For example a function that can take either an array or string as an argument, but if it's an array calls itself with each string item in the array. Very simple example:

 

function recursion($arg)
{
    if (is_array($arg)) {
        foreach ($arg as $str) {
            recursion($str);
        }
    }

    if (is_string($arg)) {
        echo $arg . "\n";
    }
}

recursion(array('foo', 'bar'));
recursion('baz');

Link to comment
Share on other sites

I wasn't talking about OOP at all with this post.

 

Recursion is purely a function that is called that within that function, that data's meets a certain critera then another function is called based upon that criteria and then sends that result back to the function which was called in the first instance (I actually missed the latter part out) and then sends the data back to the logic of your application obviously.

 

All I was asking can you put a function within a function or is this illegal syntax? That's all sorry for any confusion but that's all I was asking.

Link to comment
Share on other sites

No problem, and it turns out you can. I didn't realise this before, which is why I suggested you're likely talking about classes due to the similar syntax. Once you've called the parent function you're also able to call the child function directly:

 

function foo()
{
    function bar()
    {
        echo 'baz';
    }
}

foo();
bar(); // echoes "baz"

 

In response to your original question though, you don't need to define a function within a function to call it. Functions can call any functions that have been defined.

 

Recursion is purely a function that is called that within that function, that data's meets a certain critera then another function is called based upon that criteria and then sends that result back to the function which was called in the first instance (I actually missed the latter part out) and then sends the data back to the logic of your application obviously.

 

Recursion is a function calling itself. Doesn't neccesarily need to return anything or call any additional functions, just call itself.

Link to comment
Share on other sites

Much appreciate that reply as I said originally!

 

Haha I do that sometimes myself no worries at all, I mean think someones talking in one context and means another lol.

 

But no I every time I have tried doing that myself it never seems to work but going to give that another bash nesting functions, it makes sense if the child function as you so rightly put it isn't going to get called from outside the parent function, makes sense I suppose.

 

It's just due to one not working before I though it was incorrect syntax in a book, but will do an exact example but by nesting them to the example I was working on before, the reason why I asked this question.

 

Thanks ever so much again!

Jeremy.

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.