Jump to content


Photo

urgent: ouput buffers, includes and globals what's wrong here?


  • Please log in to reply
2 replies to this topic

#1 BrianPeiris

BrianPeiris
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 25 October 2006 - 08:01 AM

Hello everybody,
I'm new here but I'm a novice with PHP. I've been racking my brain for the past couple of hours with this problem, so I hope you can help me out.

So I've got two PHP files, one file (test1.php) includes the parsed contents of the other (test2.php) using the output buffer functions (ob_start, ob_get_contents, ob_end_clean).
The special condition is that test2.php uses a global variable in its code.
It seems to work, except when I refactor the code in test1.php into a function, it stops working.

Here's a test case:
http://brianpeiris.c...est1.php?work=1 - This link shows the output of the working code.
http://brianpeiris.c...est1.php?work=0 - This link shows the output of the broken code.
Here are the source files:
http://brianpeiris.c...t/test1.php.txt
http://brianpeiris.c...t/test2.php.txt

The problem occurs on both PHP4 and PHP5.

TIA, Brian.

#2 ksteuber

ksteuber
  • Members
  • PipPipPip
  • Advanced Member
  • 32 posts
  • LocationCalifornia, USA

Posted 25 October 2006 - 06:57 PM

Ok
I think I've figure this one out (it took me a while)

Let me rewrite your code the way it is executed:

<?php
$file='test2.php';
function get_include_contents($filename) 
{
	ob_start();
        //include($file);
        $tempVar = " HELLO!";  //This varible is defined in the function definition get_include_contents
        function doTest()
        {
	       global $tempVar;
	       echo $tempVar;
        }
        doTest();
	$contents = ob_get_contents();
	ob_end_clean();
	return $contents;
}


if($_GET['work'])
{
	echo "This works!";
	ob_start();
	//include($file);
        $tempVar = " HELLO!"; //This variable is defined outside of any function definitions in test1.php
        function doTest()
        {
	       global $tempVar;
	       echo $tempVar;
        }
        doTest();
	$contents = ob_get_contents();
	ob_end_clean();
}
else
{
	echo "This doesn't work!";
	$contents = get_include_contents($file);
}
echo($contents);
?>

Now when you look at it this way, it becomes clear that inside test1.php:get_include_contents:doTest,
global $tempVar;
is looking to see if $tempVar has been defined in test1.php. Since it is not, $tempvar continues to equal "".

Did I say that right?

#3 BrianPeiris

BrianPeiris
  • New Members
  • Pip
  • Newbie
  • 8 posts

Posted 25 October 2006 - 08:30 PM

AH! you are absolutely correct. well done, that makes things much clearer!
In fact, thanks to you, I found a solution:

<?php
$_GET['work']=0;
$file='test2.php';
function get_include_contents($filename) 
{
	ob_start();
        //include($file);
        $GLOBALS['tempVar'] = " HELLO!";
        function doTest()
        {
	       global $tempVar;
	       echo $tempVar;
        }
        doTest();
	$contents = ob_get_contents();
	ob_end_clean();
	return $contents;
}


if($_GET['work'])
{
	echo "This works!";
	ob_start();
	//include($file);
        $GLOBALS['tempVar'] = " HELLO!";
        function doTest()
        {
	       global $tempVar;
	       echo $tempVar;
        }
        doTest();
	$contents = ob_get_contents();
	ob_end_clean();
}
else
{
	echo "This doesn't work!";
	$contents = get_include_contents($file);
}
echo($contents);
?>

Now 'tempVar' is truly defined in the global scope and not just in the scope of 'get_include_contents'.
Thanks again!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users