BrianPeiris Posted October 25, 2006 Share Posted October 25, 2006 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) [i]includes[/i] 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:[url=http://brianpeiris.com/test/test1.php?work=1]http://brianpeiris.com/test/test1.php?work=1[/url] - This link shows the output of the working code.[url=http://brianpeiris.com/test/test1.php?work=0]http://brianpeiris.com/test/test1.php?work=0[/url] - This link shows the output of the broken code.Here are the source files:[url=http://brianpeiris.com/test/test1.php.txt]http://brianpeiris.com/test/test1.php.txt[/url][url=http://brianpeiris.com/test/test2.php.txt]http://brianpeiris.com/test/test2.php.txt[/url]The problem occurs on both PHP4 and PHP5.TIA, Brian. Link to comment https://forums.phpfreaks.com/topic/25028-urgent-ouput-buffers-includes-and-globals-whats-wrong-here/ Share on other sites More sharing options...
ksteuber Posted October 25, 2006 Share Posted October 25, 2006 OkI think I've figure this one out (it took me a while)Let me rewrite your code the way it is executed:[code]<?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);?>[/code]Now when you look at it this way, it becomes clear that inside test1.php:get_include_contents:doTest,[code]global $tempVar;[/code]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? Link to comment https://forums.phpfreaks.com/topic/25028-urgent-ouput-buffers-includes-and-globals-whats-wrong-here/#findComment-114372 Share on other sites More sharing options...
BrianPeiris Posted October 25, 2006 Author Share Posted October 25, 2006 AH! you are absolutely correct. well done, that makes things much clearer!In fact, thanks to you, I found a solution:[code]<?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);?>[/code]Now 'tempVar' is truly defined in the global scope and not just in the scope of 'get_include_contents'.Thanks again! Link to comment https://forums.phpfreaks.com/topic/25028-urgent-ouput-buffers-includes-and-globals-whats-wrong-here/#findComment-114437 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.