Re: Calling functions recursively: recursive PHP functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Roberto & Jason thank you for your comments.

Please note the question at the end of this email.

I searched on "recursive PHP functions" and found: 
[thelist] recursive PHP functions
Rob Wilson thelist at lists.evolt.org
Mon Sep 16 11:00:01 2002

    * Previous message: [thelist] recursive PHP functions
    * Next message: [thelist] pop up window for image without MSIE image
resize
    * Return to the message index sorted by: [ date ] [ thread ] [
subject ] [ author ]

That should read

<?php
function recursive ($n)
{
   	if ($n > 10)
   	{
   		return $n;
	}
	echo "N =" . $n . "<BR>";
	$n++;
	return recursive ($n);
}
echo "The result:".recursive(1)."<br>";
?>

With the important line being the :

return recursive ( $n );

This allows you to unwind the recursion correctly.

HTH

Rob
*************************************************************
Under www.zend.com/manual   chapter 8 "Variable scope" a mention of
recursion:
 Static variables also provide one way to deal with recursive functions.
A recursive function is one which calls itself. Care must be taken when
writing a recursive function because it is possible to make it recurse
indefinitely. You must make sure you have an adequate way of terminating
the recursion. The following simple function recursively counts to 10,
using the static variable $count to know when to stop:

<?php
function Test()
{
    static $count = 0;

    $count++;
    echo $count;
    if ($count < 10) {
        Test ();
    }
    $count--;
}
?>
***************************************************************************
Interesting but I am still lost as I need some help with
methods/functions within objects:
In the above example do I need to access Test() by:

    if ($count < 10) {
        this->Test ();

Comments please.
Kevin Gordon





-- 
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [PHP Users]     [Postgresql Discussion]     [Kernel Newbies]     [Postgresql]     [Yosemite News]

  Powered by Linux