Page 1 of 1

Noobie Question on Returing values--I'm going insane

Posted: Tue Jun 05, 2007 12:22 am
by bps67925
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi all,

Right now I have a function (defined below) that searches for multiple periods in a string.  i.e. "blah.blah..blah" fails because two consecutive periods are found.  Heres my code:

Code: Select all

function multiplePeriods($string){
		$cuts = explode('.', $string, 2);
			if($cuts[0] != NULL and $cuts[1] != NULL){
				$char = substr($cuts[1], 0,1);
					if(strcmp($char, '.')==0){
						print "returned 1";
						$f = "<td class=\"fail\">FAIL!</td></tr>";
						return $f;
					}else{
						multiplePeriods($cuts[1]);
					}
			}else{
				print "returned zero";
				$p = "<td class=\"pass\">PASS!</td></tr>";
				return $p;
			}
	}
and the function call.....

Code: Select all

$check[5] = multiplePeriods($slices[0]);
		echo $check[5];
The function is defined above the function call. Yet when I run the code, NOTHING is returned to $check[5]. I'm really at a loss and I'm practically pulling out my hair on this one. I'm sure its a simple noobie problem and if anyone could help I'd REALLY appreciate it!


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Tue Jun 05, 2007 12:23 am
by bps67925
forgot to mention, sorry, the variable $slices[0] is set to "s.f.dsfd.."

Posted: Tue Jun 05, 2007 12:41 am
by feyd
Is there a reason the recursive call's return value isn't captured?

Posted: Tue Jun 05, 2007 2:04 am
by bps67925
To be honest, I'm not even sure what that means.

Posted: Tue Jun 05, 2007 2:27 am
by John Cartwright

Code: Select all

multiplePeriods($cuts[1]);
should be

Code: Select all

return multiplePeriods($cuts[1]);
Think about it for a couple minutes, if you do not return the resursive function you are simply rerunning the function over and return its data, which ends up nowhere. Did that make sense?

Posted: Tue Jun 05, 2007 2:28 am
by bps67925
ahhh, alrighty, makes a lot more sense now, thanks a lot.