в EX1return false;
не был достигнут, потому что return
выходит из функции вы в настоящее время работаете. Так вызов $this->redirect()->toRoute(..)
будет работать, то выполнение завершится для этой функции.
в ex2, вы определяете функцию myTest()
, поэтому return $this->redirect()->toRoute(..);
выходит из myTest()
функции с возвращаемым значением toRoute()
.
Затем следующая строка кода, return false
запускает и завершает функцию, в которой она находится, со значением false
.
Как только вы вызываете return, любой код, следующий за этим оператором, будет проигнорирован. Похоже, как инструкция break;
или continue;
в цикле for.
Вы должны добавить логику, как заявление if
или switch
и определить, хотите ли вы return false
или return $this->redirect()->toRoute(..);
.
Например:
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();