jimfog Posted March 7, 2012 Share Posted March 7, 2012 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? Link to comment Share on other sites More sharing options...
boen_robot Posted March 7, 2012 Share Posted March 7, 2012 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 More sharing options...
jimfog Posted March 7, 2012 Author Share Posted March 7, 2012 oh...how did i miss that little detail. Thanks. Link to comment Share on other sites More sharing options...
justsomeguy Posted March 7, 2012 Share Posted March 7, 2012 It's not a very good idea to define a function inside another function. If you execute output_week more than once you'll get a fatal error because you're trying to declare a function that already exists. Link to comment Share on other sites More sharing options...
jimfog Posted March 8, 2012 Author Share Posted March 8, 2012 So, in other words, i should define the inner function outside if output_week. Link to comment Share on other sites More sharing options...
boen_robot Posted March 8, 2012 Share Posted March 8, 2012 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 More sharing options...
jimfog Posted March 8, 2012 Author Share Posted March 8, 2012 ...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 More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.