CodeIgniter Forums
Sending variables between 2 functions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Sending variables between 2 functions (/thread-7309.html)



Sending variables between 2 functions - El Forum - 04-03-2008

[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?


Sending variables between 2 functions - El Forum - 04-03-2008

[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.


Sending variables between 2 functions - El Forum - 04-03-2008

[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


Sending variables between 2 functions - El Forum - 04-03-2008

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