0

I am trying to simplify my code, I call the ob_start(); and ob_get_clean(); about 20 times, so to simplify the code, I have changed it from

ob_start();
  include ROOT."/functions/generate/header.php";
$header= ob_get_clean();    

To $header=get_content(ROOT."/functions/generate/header.php");

with the function as:

function get_content($file){
    ob_start();
        include $file;
    return ob_get_clean();
}

But since turning the procedure into a function, I am no longer able to access the variables in the main form form the form I am calling like the header.php file. The variables are not available when using the function, but if I use the longer version it allows me to access the variables.

Is there a reason for this or someway to work around it?

Marcel
  • 874
  • 1
  • 14
  • 28
  • two solutions: a: provide the variables as parameters to your function. b: use the `global` keyword. – Gerald Schneider Mar 17 '16 at 11:04
  • Having a file which produces output ***and*** defines variables at the same time is madness, precisely because of this. You should not write code that must be used with `ob_start`. Separate concerns and put your output producing code in one file and your function/class/variable declaring code in another. Proper encapsulation into function/classes also does wonders. – deceze Mar 17 '16 at 11:07

0 Answers0