Jump to content

Fuctions that return a value..


Recommended Posts

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.

Link to post
Share on other sites

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);

Link to post
Share on other sites

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?

Link to post
Share on other sites

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... :)

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...