Cannot redeclare function php

pindol picture pindol · Jun 7, 2012 · Viewed 113.4k times · Source

I have a function called parseDate, but when i call it on my php page (it's a joomla component page) I get Fatal error: Cannot redeclare parsedate() (previously declared in templates/ja_zeolite/assets/functions.php:2) in templates/ja_zeolite/assets/functions.php on line 21

line 2 is function parsedate($data) and line 21 is } (end of function). The function is:

function parseDate($date){
$items = explode('.', $date);
switch($items[1]){
    case 1: $mese = 'Gen'; break;
    case 2: $mese = 'Feb'; break;
    case 3: $mese = 'Mar'; break;
    case 4: $mese = 'Apr'; break;
    case 5: $mese = 'Mag'; break;
    case 6: $mese = 'Giu'; break;
    case 7: $mese = 'Lug'; break;
    case 8: $mese = 'Ago'; break;
    case 9: $mese = 'Set'; break;
    case 10: $mese = 'Ott'; break;
    case 11: $mese = 'Nov'; break;
    case 12: $mese = 'Dic'; break;
    default: $mese = '---';
}
$data_corretta = array(0 => $mese, 1 => $items[2]);
return $data_corretta;
}

I also tried to change name function, but it still doesn't work.

Why?

Answer

lanzz picture lanzz · Jun 7, 2012

You (or Joomla) is likely including this file multiple times. Enclose your function in a conditional block:

if (!function_exists('parseDate')) {
    // ... proceed to declare your function
}