Roy Barten Posted February 17, 2009 Share Posted February 17, 2009 I built 2 functions. I need the result of function1 into function2. The functions looks like the following: function1: function LookForReservation($calendartype,$startnumber)//kalendertype en startnummer worden meegegeven { include ("array.php");//array openen foreach($array as $val)//array aflopen { $number = $startnumber; //number = startnumber $s_date = strtotime($val['start']);//startdatum in gevonden array converteren naar date $e_date = strtotime($val['end']); $date = ConvertToDateTime($number,$calendartype);//gekozen datum converteren naar date, uitkomst is $date if($date > $s_date && $date < $e_date) //als de gekozen datum tussen de start en enddate ligt { $day = $startnumber + 1; //1 waarde optellen bij het startnummer. $date = ConvertToDateTime($day,$calendartype);//deze opgetelde waarde in $date zetten $countrows = 1;//de rijenteller al op 0 zetten $countres++;//de reserveringsteller optellen $id = $val['id'];//reserveringsid ophalen return $id; return $date;//de $date returnen zodat deze waarde opgevraagd kan worden in een andere functie return $countrows; return $countres; return $day; return $s_date; return $e_date; return $val['id']; DisplayDurationReservation($id,$calendartype);//vervolgens de display regelen } } } function2: function DisplayDurationReservation($id,$calendartype) { echo $id; while($date > $s_date && $date < $e_date)//zolang de datum er nog steeds tussenin valt { foreach($array as $val)//array aflopen { if ($val['id'] == $id){ echo "test"; if ($calendartype == "week"){//kalendertype bekijken $day = $day + 1;//een uurtje optellen bij de datun ($date) voor de volgende test in de loop $date = ConvertToDateTime($day,$calendartype); $countrows++;//de urenteller optellen echo $countrows; } if ($calendartype == "month"){ $day++; ConvertToDateTime($number, $calendartype); $countdays++; } } } //$counthours is nu het aantal uren, of het aantal rijen dat samengevoegd moet worden. } I need the variable $id but if i echo this, the variable is still empty. What do i wrong? Quote Link to comment https://forums.phpfreaks.com/topic/145558-send-variable-trough-function/ Share on other sites More sharing options...
.josh Posted February 17, 2009 Share Posted February 17, 2009 You can only return something once. When a return is triggered, everything past that return is not executed. return means that's it, function is done, this is being returned. So when it reaches that first 'return $id', all those other returns never get executed, and your function2 does not get called. If you want to return more than one variable, put all your variables into an array and return the array. If you want to execute another function inside the function, put it before the return. Quote Link to comment https://forums.phpfreaks.com/topic/145558-send-variable-trough-function/#findComment-764155 Share on other sites More sharing options...
cola Posted February 17, 2009 Share Posted February 17, 2009 Here examlpe. function LookForReservation($calendartype,$startnumber)//kalendertype en startnummer worden meegegeven { include ("array.php");//array openen foreach($array as $val)//array aflopen { $number = $startnumber; //number = startnumber $s_date = strtotime($val['start']);//startdatum in gevonden array converteren naar date $e_date = strtotime($val['end']); $date = ConvertToDateTime($number,$calendartype);//gekozen datum converteren naar date, uitkomst is $date if($date > $s_date && $date < $e_date) //als de gekozen datum tussen de start en enddate ligt { $day = $startnumber + 1; //1 waarde optellen bij het startnummer. $date = ConvertToDateTime($day,$calendartype);//deze opgetelde waarde in $date zetten $countrows = 1;//de rijenteller al op 0 zetten $countres++;//de reserveringsteller optellen $id = $val['id'];//reserveringsid ophalen return array($id,$date,$countrows,$countres,$day,$s_date,$e_date,$val['id']); DisplayDurationReservation($id,$calendartype);//vervolgens de display regelen } } } function function2 { list($id,$date,$countrows,$countres,$day,$s_date,$e_date,$val['id'])= LookForReservation($calendartype,$startnumber); echo "$id, $date etc); } Quote Link to comment https://forums.phpfreaks.com/topic/145558-send-variable-trough-function/#findComment-764159 Share on other sites More sharing options...
.josh Posted February 17, 2009 Share Posted February 17, 2009 cola your example won't work. function2 is not calling function1 so it's not receiving that returned info. It gets returned to wherever function1 was called. Also, your example has the return before the function2 call, so that call never gets made. Quote Link to comment https://forums.phpfreaks.com/topic/145558-send-variable-trough-function/#findComment-764165 Share on other sites More sharing options...
cola Posted February 17, 2009 Share Posted February 17, 2009 It is need to work u did not write what is error. Here some modification i think that this work function LookForReservation($calendartype,$startnumber)//kalendertype en startnummer worden meegegeven { include ("array.php");//array openen foreach($array as $val)//array aflopen { $number = $startnumber; //number = startnumber $s_date = strtotime($val['start']);//startdatum in gevonden array converteren naar date $e_date = strtotime($val['end']); $date = ConvertToDateTime($number,$calendartype);//gekozen datum converteren naar date, uitkomst is $date if($date > $s_date && $date < $e_date) //als de gekozen datum tussen de start en enddate ligt { $day = $startnumber + 1; //1 waarde optellen bij het startnummer. $date = ConvertToDateTime($day,$calendartype);//deze opgetelde waarde in $date zetten $countrows = 1;//de rijenteller al op 0 zetten $countres++;//de reserveringsteller optellen $id = $val['id'];//reserveringsid ophalen $val=$val['id']; return array($id,$date,$countrows,$countres,$day,$s_date,$e_date,$val); DisplayDurationReservation($id,$calendartype);//vervolgens de display regelen } } } function function2 { list($id,$date,$countrows,$countres,$day,$s_date,$e_date,$val)= LookForReservation($calendartype,$startnumber); echo "$id, $date"; } Quote Link to comment https://forums.phpfreaks.com/topic/145558-send-variable-trough-function/#findComment-764168 Share on other sites More sharing options...
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.