在PHP中返回的替代方法?

I want to run two loops one after the other but it seems the return statement in the first loop ends the script:

// this runs
for ($i = 0; $i < 3; $i++) {
    try {
        foo();
        return true;
    } catch (Exception $e) {
        try {
            bar();
        } catch (Exception $e) {
            return false;
        }
    }
}


// this doesn't
for ($i = 0; $i < 3; $i++) {
    try {
        foo();
        return true;
    } catch (Exception $e) {
        try {
            bar();
        } catch (Exception $e) {
            return false;
        }
    }
}

You can't use return within a loop, it's meant for functions/class methods.

You should replace return false with break, the loop equivalent, and return true with continue. However, as John Conde noticed, you could just leave continue out in this example.

function         loop
---------------------------
return true;     continue;
return false;    break;

Remove the return true and replace it with nothing.

Or, use continue. It will restart the loop from the next iteration.

update

Replace the return false with break as mentioned in your comments