Zend framework2 zend framework 2返回$this->;rediect()

Zend framework2 zend framework 2返回$this->;rediect(),zend-framework2,Zend Framework2,例1: 例2: 当我使用ex1时,我的代码停止并返回,没有运行return false; 与ex2下面的代码相同,返回false ran。 请帮帮我,为什么会这样???在ex1中返回false。因此调用$this->redirect()->toRoute(..)将运行,然后该函数的执行将结束 在ex2中,您定义了一个函数myTest(),因此返回$this->redirect()->toRoute(…)退出myTest()函数,返回值为toRoute() 然后,下一行代码return false

例1:

例2:

当我使用ex1时,我的代码停止并返回,没有运行return false; 与ex2下面的代码相同,返回false ran。
请帮帮我,为什么会这样???

ex1
中返回falsereturn
退出当前正在运行的函数,因此无法访问code>。因此调用
$this->redirect()->toRoute(..)
将运行,然后该函数的执行将结束

ex2中,您定义了一个函数
myTest()
,因此
返回$this->redirect()->toRoute(…)
退出
myTest()
函数,返回值为
toRoute()

然后,下一行代码
return false
运行并退出它所在的函数,其值为
false

一旦调用return,该语句后面的任何代码都将被忽略。有点像一个
break
继续语句

您需要添加逻辑,如
if
switch
语句,并确定是
返回false
还是
返回$this->redirect()->toRoute(…)

例如:

public function myTest(){
     return $this->redirect()->toRoute(..);
}

// do something ...

myTest();
return false;
public function myTest(){
     return $this->redirect()->toRoute(..);
}

// do something ...

myTest();
return false;
function someFunction() {       // someFunctionCalled
    if (codingIsFun) {          // Coding is fun
        $foo = myTest();        // $foo is true, since myTest() returns true.
        return $foo;            // Exit "someFunction()" with a return value of $foo (true)
                                // any remaining code in "someFunction()" will not be executed.
    }

    // Some people will put this line in an "else" block,
    // but it isn't necessary, this code will only execute if 
    // coding is not fun.
    return false; // Coding is not fun.
}

function myTest() {
    return true;
}


// Call someFunction, if coding is fun, $isCodingFun will == true,
// If not, $isCodingFun will == false.
$isCodingFun = someFunction();