出于某种原因,我无法运行change()
函数以及停止后的所有内容。我尝试使用$this->change()
,但效果是一样的。如果我在php文件中运行该函数,它的工作和num正在改变。
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
}
}
答案 0 :(得分:4)
您可能最好调用私有或公共函数来处理数据(或者对于更复杂/涉及的进程库)。
即
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;
}
}
}
答案 1 :(得分:3)
大声笑,你正在尝试在函数内部编写函数。
尝试使用此代码运行您的课程
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;
}
}
}