Hello all,

I have an issue regarding using variables from my global.php in a hiarachy of documents.

I will explain how this works. I have my index.php in the root of my server, I then include the global.php file and from then on I include a handle.php file for the related page and from that file I usually include another file realted to what part of that page they require.

Okay so far I have created the members system (register login verify etc.) that works fine but the issue comes when running a query in the global.php to get all of the users details:

if( @mysql_num_rows( $query_user ) < 1 ) {
} else {
	$row = mysql_fetch_array( $query_user );
	$query_user2 = mysql_query("SELECT * FROM `users` WHERE `id` = '" . $row['id'] . "'");
	while( $ui = mysql_fetch_array( $query_user2 ) ) {
		$username = $ui['username'];
		global $username;
		$id = $ui['id'];
		global $id;
		$email = $ui['email'];
		global $email;

I can use the variables created in the original index.php, the issue is using them in the included documents. Functions created in the global.php work fine but the varibales (including others) don't. I have tried declairing them global but that doesn't seem to work.


global $username;

Any help would be great,

Edited by Froger93: n/a

8 Years
Discussion Span
Last Post by chrishea

To verify what is working at each step, you can use get_defined_vars(); to dump the variables to see what is defined and what isn't. You can experiment until it works.


Okay thanks thats halped allot, I have figured out that most of the variables wont display because I have included the documents using a function to make sure the file exists and if it doesn;t display a 404 handler.

Is there any way I can use all the variables from outside of the function inside this function?


The simplest way (but would probably require some rework on your part) would be to use session variables that you can access anywhere. If you don't do that, then you will probably need Global definitions for all of the external variables that you will be using in the function.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.