Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Array info dissapears when using functions

Status
Not open for further replies.

cadbilbao

Programmer
Apr 9, 2001
233
ES
Hi.

I created these PHP scripts, in order to execute 'foo.php'
by using 'ob_start' via another script ('admin.php').

'foo.php' requires some libraries ('functions_for_foo.php'
where some arrays are handled) and 'admin.php' requires other
libraries ('unctions_for_admin.php').

--------{array_info.php}-----------------
$myList = array ("John", "Mary");
-----------------------------------------

--------{foo.php}------------------------
require_once("array_info.php");
require_once("functions_for_foo.php");
manageArray();
-----------------------------------------

--------{functions_for_foo.php}----------
function manageArray()
{
global $myList;
print_r($myList);
}
-----------------------------------------

--------{admin.php}----------------------
// OPTION 1
// require_once("functions_for_admin.php");
// doInclude();
// END OPTION 1


// OPTION 2
ob_start();
chdir('/usr/local/httpd/htdocs/');
include('/usr/local/httpd/htdocs/foo.php');
$out = ob_get_contents();
ob_end_clean();

print "<li>GOT CONTENTS:\n";
print $out;
// END OPTION 2
------------------------------------------

--------{functions_for_admin.php}---------
function doInclude()
{
ob_start();
chdir('/usr/local/httpd/htdocs/');
include('/usr/local/httpd/htdocs/foo.php');
$out = ob_get_contents();
ob_end_clean();

print "<li>GOT CONTENTS:\n";
print $out;
}
------------------------------------------

If I execute 'admin.php' with 'OPTION 2' (without calling
'functions_for_admin.php'), I can display info ($out) from array,
but I use 'OPTION 1' (calling 'functions_for_admin.php'),
this info dissapears.

Just the same if I don't call 'functions_for_foo.php' within
'foo.php' (got array info if I don't call functions).

Any similar experience?

Thank you very much.
 
It looks like a variable scoping problem to me.

The nice thing about PHP's include() function is that you can duplicate what it does by hand by typographically copying script code to replace the include() invocation.

*First, let's assume all the <?php and ?> tags are where they're supposed to be in the running code.*

Take a look at option 2:

Code:
// OPTION 2
ob_start();
chdir('/usr/local/httpd/htdocs/');
include('/usr/local/httpd/htdocs/foo.php');
$out = ob_get_contents();
ob_end_clean();

Commend out all the include() invocations and replace them with the included code. You get:
Code:
// OPTION 2
ob_start();
chdir('/usr/local/httpd/htdocs/');
//include('/usr/local/httpd/htdocs/foo.php');
	//require_once("array_info.php");
		$myList = array ("John", "Mary");
	//require_once("functions_for_foo.php");
		function manageArray()
		{
    		global $myList;
		    print_r($myList);
		}

	manageArray();

$out = ob_get_contents();
ob_end_clean();

print "<li>GOT CONTENTS:\n";
print $out;
// END OPTION 2

*NOTE: I commented out each include, then indented the included code one tab-space below it*

$myList is created in the topmost variable scope and printed from a function that references the topmost-scoped variable $mylist using the "global" modifier. That will work.

Now, let's take option 1:

Code:
// OPTION 1
// require_once("functions_for_admin.php");
// doInclude();
// END OPTION 1

and expand it:

Code:
// OPTION 1
//require_once("functions_for_admin.php");
	function doInclude()
	{
	    ob_start();
	    chdir('/usr/local/httpd/htdocs/');
	    //include('/usr/local/httpd/htdocs/foo.php');
	    	//require_once("array_info.php");
	    		$myList = array ("John", "Mary");

			//require_once("functions_for_foo.php");
				function manageArray()
				{
				    global $myList;
				    print_r($myList);
				}

			manageArray();

	    $out = ob_get_contents();
	    ob_end_clean();
    
	    print "<li>GOT CONTENTS:\n";
	    print $out;
	}

doInclude();
// END OPTION 1

You have something of a mess here. You're declaring a function within a function, which is really not a smart thing to do. But you're assuming that the function managearray(), since it was defined inside doInclude() will have as its next-up scope the variable scopt of doInclude() (that is, the "global" modifier will reference a variable inside the scope of doInclude() because managearray() is defined inside doInclude()).

However, since all functions are defined to have the global scope, even if they were defined inside another class, the "global" modifier gives you the topmost scope.

Take a look at this example code:

Code:
<?php

$a = 3;

function b()
{
	$a = 4;
	
	function c()
	{
		global $a;
		
		print $a;
	}
}

b();

c();
?>

(You have to run b() first. c() will not exist until you do.)

When you run it, you would expect that the invocation of c() would output 4. It does not -- it outputs 3 because the "global"ly-modified $a references the topmost scope, not the function-level $a inside b().

Similarly, your code is trying to reference a variable $myList in the topmost scope when it was defined in the function-level scope of doInclude().



Want the best answers? Ask the best questions! TANSTAAFL!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top