Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/145558-send-variable-trough-function/
Share on other sites

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.

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

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.

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

}

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.   Restore formatting

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.