martin.kahil 0 Posted November 14, 2007 Report Share Posted November 14, 2007 Hi, i'm new here and new to PHP. and i've read a large part of the PHP toturial, and have just one qustion.how do i write a function that returns a value. that i can do something like this below.. it actually works, but seems strange to me, and i'm sure the there is a better way to write it, were i declare that its a function that returns values.thankx! function days_in_month($month){if ($month==1 or $month==3 or $month==5 or $month==7 or $month==8 or $month==10 or $month==12) $monthlen=31;else if($month==4 or $month==6 or $month==9 or $month==11) $monthlen=30;else if(($year%4)==0) $monthlen=29;else $monthlen=28;return $monthlen;}$days=days_in_month(4); and the variable $days will be set to 30. Quote Link to post Share on other sites
Ingolme 1,035 Posted November 14, 2007 Report Share Posted November 14, 2007 You can try this: function days_in_month($month,$year) {/* $year is necessary just in case it's a leap year */$d = mktime(0,0,0,1,$month,$year);//This function creates a PHP date object$days = date("t",$d);// 't' returns the days of a given monthreturn $days;}$days = days_in_month(4,2000); Quote Link to post Share on other sites
martin.kahil 0 Posted November 16, 2007 Author Report Share Posted November 16, 2007 Thanks.. I figured out the $year thing alone.. but my question was someing else.. I want to to know if there is something in the function header that declares that its a function that returns a value or not...Like in Visual Basic Function days_in_month(month int, year int) int the last "int" says that its a function that returns an intenger. Is there something like it in PHP? Quote Link to post Share on other sites
Ingolme 1,035 Posted November 16, 2007 Report Share Posted November 16, 2007 Just the return $days; line. If that line is present the function returns something, if not, nothing is returned. Quote Link to post Share on other sites
SpOrTsDuDe.Reese 1 Posted November 16, 2007 Report Share Posted November 16, 2007 You can always use the echo statement. Quote Link to post Share on other sites
martin.kahil 0 Posted November 17, 2007 Author Report Share Posted November 17, 2007 OK, Thanks everyone! Quote Link to post Share on other sites
martin.kahil 0 Posted November 17, 2007 Author Report Share Posted November 17, 2007 Ingolme for your information, and everybody else:In the function mktime you write first the month then the day and in the end the year!witch means that it has to be like this: mktime(0,0,0,$month,1,$year);//this is rightmktime(0,0,0,1,$month,$year);//this is wrong! A litle difrence, but can drive you crazy untill you find it... Quote Link to post Share on other sites
Synook 47 Posted November 18, 2007 Report Share Posted November 18, 2007 Hey what is wrong with American notation? Quote Link to post Share on other sites
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.