D7: function calls too early (before DOCTYPE) and mess my page I have included a class file in my module:

function MYMODULE_init() {
  require_once(drupal_get_path('module','MYMODULE')."/jcart/jcart.php");
}


where I declare a function

public function display_container(){

  echo "&lt;div id='jcart' class='jcart1'&gt;";
  echo "&lt;/div&gt;";      
  echo "&lt;div id='jcart-tooltip'&gt;&lt;/div&gt;\n";

}


I called this function in another module (they are all the same level default/modules)

The problem is this func displays its content before the html doctype causing the layout to mess:

&lt;div class='checky'&gt;&lt;div id='jcart' class='jcart1'&gt;&lt;/div&gt;&lt;/div&gt;&lt;div id='jcart-tooltip'&gt;&lt;/div&gt;
&lt;!DOCTYPE html&gt;
You need to return your HTML string as the page callback rather than outputting it directly...otherwise what your doing is incompatible with the the Drupal theme system, and is why you're getting the problem you're getting at the moment.

Without seeing all of your code its impossible to debug this completely but suffice to say that in whichever page callback function you're calling display_container() you want to do this instead:

// Start the output buffer
ob_start();

// This will perform the echo to your new local buffer, not the screen buffer.
display_container();

// Get the temporary buffer content into a variable.
$output = ob_get_clean();

// Return the page callback to Drupal as normal
return $output;


If you're in a block view (which I seem to recall you might be from one of your previous questions) that last line would be:

$block['content'] = $output;


The other, perhaps simpler method here, would be to simply change your display_container() function to return the string rather than output it directly:

public function display_container(){
  $container = "&lt;div id='jcart' class='jcart1'&gt;";
  $container .= "&lt;/div&gt;";      
  $container .= "&lt;div id='jcart-tooltip'&gt;&lt;/div&gt;\n";

  return $container;
}