springo Posted April 7, 2007 Share Posted April 7, 2007 Hi, I'm trying to create a function that adds tabs to the HTML output so that it looks easier to read when you look at the source code of the page. However, I get an error: "Fatal error: Cannot redeclare tab() (previously declared in C:\Program Files\xampp\htdocs\parser\functions.php:3) in C:\Program Files\xampp\htdocs\functions.php on line 30" (where line 3 is the first line of this function and line 30 is the last one). I don't know if there are any other mistakes in my code that might make it crash after solving this one, but I'd need help at least with this issue. Here is the source of the function: <?php function tab(&$str) { global $tab_level; if (!($str_array = preg_split("/\\n</",$str))) return false; for ($index = 0; $index < count($str_array); $index++) { if (preg_match("/^\//",$str_array[$index])) { $tab_level--; } else { $tab_level++; } if ($tab_level) { for ($tabbed = 0; $tabbed < $tab_level; $tabbed++) { $tab .= "\t"; } } $str_array[$index] = $tab.$str_array[$index]; if (preg_match("/<\//",$str_array[$index])) $tab_level--; } implode($str,$str_array); return true; } Thank you very much for your help. PS: Sorry if you don't like the code, but I'm a newbie to PHP and still not very familiar with all functions. Link to comment https://forums.phpfreaks.com/topic/46044-function-previously-declared/ Share on other sites More sharing options...
redking Posted April 7, 2007 Share Posted April 7, 2007 What that means is that you are defining the function tab() twice. this file has the function tab() C:\Program Files\xampp\htdocs\functions.php so does this one C:\Program Files\xampp\htdocs\parser\functions.php delete the function in one or the other or don't include one of the files Link to comment https://forums.phpfreaks.com/topic/46044-function-previously-declared/#findComment-223715 Share on other sites More sharing options...
springo Posted April 7, 2007 Author Share Posted April 7, 2007 Whoops that was a typo, I forgot the "parser" in the second one, but it's the same file. Actually, the error is: "Fatal error: Cannot redeclare tab() (previously declared in C:\Program Files\xampp\htdocs\parser\functions.php:3) in C:\Program Files\xampp\htdocs\parser\functions.php on line 30" Link to comment https://forums.phpfreaks.com/topic/46044-function-previously-declared/#findComment-223731 Share on other sites More sharing options...
redking Posted April 7, 2007 Share Posted April 7, 2007 You probably included the same file more than once by accident, use include_once or require_once wherever you included the function.php file. or make sure it is only included once, it may be inside of a loop or somthing. Link to comment https://forums.phpfreaks.com/topic/46044-function-previously-declared/#findComment-223735 Share on other sites More sharing options...
springo Posted April 7, 2007 Author Share Posted April 7, 2007 Just realized I was including the file containing the function and another which itself included the one with the function. Thanks! (Function doesn't work but at least I get an output so let's start working on it!) Link to comment https://forums.phpfreaks.com/topic/46044-function-previously-declared/#findComment-223745 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.