Page 1 of 1

problem with returning values

Posted: Wed Jun 08, 2005 10:02 pm
by jaymoore_299
I have a recursive function to which i pass a file that contains characters like %%%% (there are 3 of them) which I replace with dynamic content.

it roughly looks like this:

Code: Select all

function recursive($array)
{
if %%%% exists
 {
 replace it
 recursive($array)
 }
else
 {
 print_r $array
 return $array
 }
< marker 1>
}
When I print the array at else, the recursive array is just as I expected. However, nothing is returned.
When I put a return $array at marker 1, the file is returned, however it is not the completed array.

Why does this happen?

How do I return a value at else?

Posted: Wed Jun 08, 2005 10:11 pm
by jaymoore_299
and I'm wondering why a return statement at marker 1 would execute at all since if the file did have a %%%%, the first case would be executed, otherwise all others would go to the else case. Anyone have any ideas?

Posted: Wed Jun 08, 2005 10:25 pm
by jaymoore_299
here is an example,
eventually 4 would be incremented to over 100 so that the else case should be executed, but the return 8888 is never executed. I do see the statement:
"8888 should print after this" however. Why isn't the return executed?

Code: Select all

function square($num)
{
if ($num<100)
{
$tt=$num + 10;
print "</br>".$tt;
square($tt);
}
else
{
print "8888 should print after this";
return 8888;
}
}
$test=square(4);  // outputs '16'.
print $test;

Posted: Thu Jun 09, 2005 12:37 am
by Syranide
because you don't return the square()-value, when the if is true, you return null.