Welcome Guest, Not a member yet? Register   Sign In
Sending variables between 2 functions
#1

[eluser]luukskywalker[/eluser]
i have a small question

i have 2 functions -> Function_A() and Function B()
and i want to send a variable from function a to function b.
I want something like this(this is just an example):
Code:
Function_a(){
  
  $foo = "1";
  $this->_function_b($foo);
}

Function_b(){
   if($foo){
      
     echo $foo+1;

   }else{

     $foo = "10";
     echo $foo;
   }
}

is this possible?
#2

[eluser]xwero[/eluser]
That is some bad example code Smile

If you have methods (functions) in a class you can do
Code:
class Someclass
{
   var $foo;

   function a()
   {
      $this->foo = 1
      $this->b();
   }

   function b()
   {
      if(is_numeric($this->foo))
      {
          return $this->foo + 1;
      }
      else
      {
          return 10;
      }
   }
}

if it are separate functions you can only pass values by using parameters. You could use a global but that is bad practice.
#3

[eluser]luukskywalker[/eluser]
Nice this works fine.

Thank you.

P.s. sorry for the bad example code. I changed it like a thousand times to make clear what i meantSmile
I kinda became suckySmile
#4

[eluser]webthink[/eluser]
Since you asked about sending a variable between functions you should probably see this example as well (not everything should be set as a class attribute)
Code:
class Someclass
{

   function a()
   {
      $foo = 1
      $foo2 = $this->b($foo);
   }

   function b($foo)
   {
      if(is_numeric($foo))
      {
          return $foo + 1;
      }
      else
      {
          return 10;
      }
   }
}




Theme © iAndrew 2016 - Forum software by © MyBB