Jump to content

Function "previously declared"?


springo

Recommended Posts

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

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

 

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"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.