Function in controller

Hello,
I have a probably simple problem, but do not see the solution.
in the controller I wrote a function in an action and I can’t access the return values, because the variable is supposedly not defined…

function test(){
            $test = 1;
            return $test;
        }

        echo $test;

Can anyone help me?
Thanks and best regards
Ole

Are you aware that echo $test; is not actually in your function?

yes,

my code:

<?php
...
class AllocationsController extends AppController

{

  public function a()
  {

        ...

       function b(){
       --do something e.g.-- 
            $test = 1;
            return $test;
        }

        debug($test);
    }
}

output: null

i can not get $test outside of function b().

ok, thanks!
the solution is: i have to use references…

<?php
...
class AllocationsController extends AppController

{

function b(){
       --do something e.g.-- 
            $test = 1;
            return $test;
        }

  public function a()
  {

      $test = $this->b();

        debug($test);
    }
}

You need to put function outside function and call that function in function.

1 Like