41

I've setup a wordpress blog (I imported the db) and it's throwing this error

Fatal error: 'break' not in the 'loop' or 'switch' context in /home/kbuzz/webapps/kb_blog/wp-content/plugins/types/embedded/common/toolset-forms/lib/adodb-time.inc.php on line 1012

The code is below from line 1004 to 1013

function adodb_tz_offset($gmt,$isphp5)
{
    $zhrs = abs($gmt)/3600;
    $hrs = floor($zhrs);
    if ($isphp5) 
        return sprintf('%s%02d%02d',($gmt<=0)?'+':'-',floor($zhrs),($zhrs-$hrs)*60); 
    else
        return sprintf('%s%02d%02d',($gmt<0)?'+':'-',floor($zhrs),($zhrs-$hrs)*60); 
    break;
}
Nikhil Vaghela
  • 2,088
  • 2
  • 15
  • 30
Samuel Muiruri
  • 625
  • 1
  • 5
  • 13

4 Answers4

79

PHP 5.x.x, a break statement outside a for, foreach, while or switch statement DID NOT throw an error message and was syntactically okay.

PHP 7.0 and higher, a break statement is no longer permitted outside a for, foreach, while or switch statement and gives a fatal error.

Example code:

<?php
if (2 == 1 + 1) {
    echo "Dummy Example of break inside if condition";
    break; // - Valid in php 5.*
           // - Gives a Fatal error in PHP 7.*.*:
           // "Fatal error: 'break' not in the 'loop' or 'switch' context in ... "
}
?>
exscape
  • 2,185
  • 4
  • 21
  • 25
Basil Musa
  • 8,198
  • 6
  • 64
  • 63
  • 1
    This answer is partially correct but caused me to waste a lot of time tracking down the wrong issue. break and continue are still allowed in while loops as well. – davewhirlwind Feb 08 '17 at 20:59
  • 2
    Thanks! I was able to fix the problem by commenting out the (useless) break statement in the offending .php file. – Judah Gabriel Himango Mar 01 '17 at 15:38
  • 1
    is there an alternative then? something to prevent the rest of the script from running? – oldboy Aug 07 '17 at 19:21
  • @davewhirlwind he didn't say it wasn't allowed within while loops :) so still a correct answer. but why isn't this marked as the answer? – Bram Hammer Nov 20 '17 at 10:12
  • @Anthony you could use an if/else statement? A return false if you're in a function? etc.. multiple good options imho. – Bram Hammer Nov 20 '17 at 10:13
  • Is there a reason for this? break's commonly used in exactly this way to prevent a loop that doesn't need to run anymore. Why would the PHP team take this tool out of developer hands? – Weston C Mar 12 '18 at 09:49
  • 1
    It is still permitted inside for, while and switch statements. It is NOT permitted else where. – Basil Musa Mar 12 '18 at 10:34
30

look at the break;

replace with

return false;

in your code and it will work.

sandip
  • 533
  • 9
  • 29
Ram Kaushik
  • 341
  • 3
  • 4
  • I think this is a more general answer. With some explanation it will become the best answer. In my case I have: `function do_something() { if ($condition) { } else { break; } }` If I just remove the `break`, will be executed. With `return false;` will not be executed. – Trayan Momkov May 02 '20 at 11:31
11

Removing break fixed it

function adodb_tz_offset($gmt,$isphp5)
{
   $zhrs = abs($gmt)/3600;
    $hrs = floor($zhrs);
if ($isphp5) 
    return sprintf('%s%02d%02d',($gmt<=0)?'+':'-',floor($zhrs),($zhrs-$hrs)*60); 
else
    return sprintf('%s%02d%02d',($gmt<0)?'+':'-',floor($zhrs),($zhrs-$hrs)*60); 
}
Samuel Muiruri
  • 625
  • 1
  • 5
  • 13
0

use return false; instead of break; or exit(); or continue; This prevent the rest of the script from running but will continue the loop if at all the rule still true

katulamu
  • 9
  • 3