0

For some reason I cant run the change() function and everything after it stops. I try with $this->change() but the effect is the same. If I run the function in a php file its working and the num is changing.

class Test extends CI_Controller {

    function __construct(){
        parent::__construct();
    }

    public function home(){

        $num = 1;

        change($num);


        function change($num,$rand=false){

            echo 'inside function'; // this is not shown

            if($num < 4) {
                $rand = rand(1,9);
                $num = $num.$rand;
                change($num,$rand);
            } else {
                echo $num;
            }
        }
        echo 'done'; // this is not shown

    }

}
3
  • 1
    if your going to down vote, give a reason. Commented Oct 19, 2012 at 15:54
  • also is num supposed to be a float int type? use '.' won't make that happen that is for string concatenation Commented Oct 19, 2012 at 15:57
  • 2
    just take the function outside.... it will work like a charm. Commented Oct 19, 2012 at 16:00

2 Answers 2

4

You will probably be better off calling a private or public function to process data ( or for more complex/involved processes a library ).

ie

class Test extends CI_Controller
{
  public function __construct()
  {
      parent::__construct();
  }

  public function home()
  {
    $num = 1;

    echo $this->_change($num);
    echo 'done'; // this is not shown
  }

  // private or public is w/o the underscore
  // its better to use private so that CI doesn't route to this function
  private function _change($num, $rand = FALSE)
  {
        if($num < 4) {
            $rand = rand(1,9);
            $num = $num + $rand;
            $this->_change($num,$rand);
        } else {
            return $num;
        }
  }
}
Sign up to request clarification or add additional context in comments.

2 Comments

agreed. this is a better implementation. a private function would actually do the trick.
fufu i updated the code, since you are using recursion make sure that your function is actually creating a stack by probably using die() or var_dump in the else clause.
3

lol, you are trying to write function inside function.

try running your class with this code

class Test extends CI_Controller {

    function __construct(){
        parent::__construct();
    }

    public function home(){

        $num = 1;

        $this->change($num);
        echo 'done'; // this is not shown

    }

    public function change($num,$rand=false){

            echo 'inside function'; // this is not shown

            if($num < 4) {
                $rand = rand(1,9);
                $num = $num.$rand;
                change($num,$rand);
            } else {
                echo $num;
            }
    }

}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.