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

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
bps67925
Forum Newbie
Posts: 11
Joined: Tue May 29, 2007 10:34 pm

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

Post 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]
bps67925
Forum Newbie
Posts: 11
Joined: Tue May 29, 2007 10:34 pm

Post by bps67925 »

forgot to mention, sorry, the variable $slices[0] is set to "s.f.dsfd.."
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Is there a reason the recursive call's return value isn't captured?
bps67925
Forum Newbie
Posts: 11
Joined: Tue May 29, 2007 10:34 pm

Post by bps67925 »

To be honest, I'm not even sure what that means.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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?
bps67925
Forum Newbie
Posts: 11
Joined: Tue May 29, 2007 10:34 pm

Post by bps67925 »

ahhh, alrighty, makes a lot more sense now, thanks a lot.
Post Reply