Jump to content

function within a function not working


jimfog
 Share

Recommended Posts

here is a function which contains an inner function that does not work(the inner one) and i cannot figure out why:

function output_week() {	 global  $monthNames1, $newdate,$begweek,$endweek,$startday,$previousmonth,$nextmonth;			 $previousmonday=strtotime('previous monday' , strtotime($newdate));			 $nextsunday=strtotime('next sunday' , strtotime($newdate));			 $thismonday=strtotime('thismonday' , strtotime($newdate));			 $today=getdate();			 echo $previousmonth;		  	 echo '<ul id="days">';	 echo	   '<li><a href=" ' . $_SERVER["PHP_SELF"] ."?newdate=". $begweek . '">Previous week</a><li>';	  	 function test ()	 {		 global  $monthNames1, $newdate,$begweek,$endweek,$startday,$previousmonth,$nextmonth;			 $previousmonday=strtotime('previous monday' , strtotime($newdate));			 $nextsunday=strtotime('next sunday' , strtotime($newdate));			 $thismonday=strtotime('thismonday' , strtotime($newdate));			 $today=getdate();		if(((date("n",$nextsunday ))==($nextmonth))||(($nextmonth==3)&&(date("n",$previousmonday)==1))&&($today[wday]!==1))					{			echo '<li>'. date("d",$previousmonday).$monthNames1[date("n",strtotime($newdate))-1] .		'----'.date("d",$nextsunday ).' '.$monthNames1[date("n",strtotime($newdate))] .		'  ' . date("Y", strtotime($newdate)).'</li>';		  		} 	   elseif(((date("n",$nextsunday ))==($nextmonth))||(($nextmonth==3)&&(date("n",$previousmonday)==1))&&($today[wday]==1)) 	   {echo '<li>'. date("d",$thismonday).$monthNames1[date("n",strtotime($newdate))-1] .			   '--'.date("d",$nextsunday ).''. $monthNames1[date("n",strtotime($newdate))] .			   '  ' . date("Y", strtotime($newdate)).'</li>';}	   	   else	  	  	   {echo '<li>'. date("d",$previousmonday).	   '----'.date("d",$nextsunday ).''. $monthNames1[date("n",strtotime($newdate))-1] .			   '  ' . date("Y", strtotime($newdate)).'</li>';}	 }	   echo   '<li><a href=" ' . $_SERVER["PHP_SELF"] ."?newdate=".$endweek. '">Next week</a><li>';	   echo  '</ul>';	  }

Above, function test is not executed-any ideas why?

Edited by jimfog
Link to comment
Share on other sites

Why should it? I don't see you calling it anywhere... you're just defining it within the function. If after its declaration you have something like

           test();           echo   '<li><a href=" ' . $_SERVER["PHP_SELF"] ."?newdate=".$endweek. '">Next week</a><li>';           echo  '</ul>';

it should work.

Link to comment
Share on other sites

oh...how did i miss that little detail. Thanks.

Link to comment
Share on other sites

So, in other words, i should define the inner function outside if output_week.

Link to comment
Share on other sites

Yes, or if they're too closely coupled and the inner function doesn't make sense, make a class that holds the two functions, and make the inner function a private method.

Link to comment
Share on other sites

...make a class that holds the two functions, and make the inner function a private method.
I do not know a lot from classes yet(i still develop functionally) but it seems this a good point to begin experimenting with them.
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...