php – Cannot redeclare CheckEvenOdd()

Question:

Ошибка:

Fatal error: Cannot redeclare CheckEvenOdd() (previously declared in
D:\OpenServer\domains\localhost\functions.php:2) in
D:\OpenServer\domains\localhost\functions.php on line 8

function CheckEvenOdd($value) {
  if(($value%2) == 0) {
    return 'even';
  } else {
    return 'odd';
  }
}

How to fix? The function is not specified anywhere else. It is called like this:

$week = CheckEvenOdd(date('W'));

if($week == 'even') {
  $above = ' class="active"';
  $below = '';
} elseif($week == 'odd') {
  $below = ' class="active"';
  $above = '';
}

Answer:

One way or another, you have a file with this function connected twice, so on the second pass the interpreter tries to re-declare it, which leads to the above error. If there are self-written autoloaders, check them first; If this is not the case, make sure that you do not include the file twice or that it is not connected in a loop.

Scroll to Top