0

Is there something in php that can halt or let the script proceed if things are ok?

In my current scripts I do like this (for this example):

$fetch = false;

if($i == 10){
echo 'I is clear'
$fetch = true;
}

if($fetch){
 //Do database work here
}

echo 'This should be seen no matter what the above';

Instead of the $fetch in there, can I do something else? I don't want to stop the entire script after that like what die() or exit does.

jmenezes
  • 1,888
  • 6
  • 28
  • 44

1 Answers1

0

Here's an example that should help you:

<?php
function doLoop() {
    for($i=0;$i<100;$i++) {
        if($i != 50) {
            continue; //It's not 50, skip it
        }
        //Otherwise
        printf("Loop: $i");
    }
}
function doBreak() {
    for($i=0;$i<100;$i++) {
        if($i != 49) {
            continue; //It's not 49 yet, continue
        } //Otherwise, break
        printf("Loop: $i");
        break;
    }
}
doLoop();
doBreak();
?>

break; can be used to end a loop when a condition is met, while continue; can also be used to skip a certain value if a condition is not met. Using die(); would stop your whole script from executing, preventing it to call anything that comes after the die(); statement because that's how the execution of the scripts pretty much go, from the top to the end.