How do I declare an array as global?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

How do I declare an array as global?

Post by webcan »

Hey guys, here's a question from a newbie ;-)

If I have a function that creates an array such as: (note the $row array)

Code: Select all

function something() {
	...
	$querystring = "SELECT * FROM dbtable WHERE username = '$username' LIMIT 1;";
	$link = mysql_connect("localhost", "whatever", "whatever");
	mysql_select_db("dbname");
	$result = mysql_query($querystring);
		
	while ($row = mysql_fetch_array($result)) {
		...
	}
	...
}
And then there is a second function somewhere further down that wants to access data from the $row array, (eg. $row["name"]), how do you declare the $row array in the first function so that it is globally accessible?
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

probably easier to pass the array as parameter

Code: Select all

<?php
function funcA($arrayParamA)
{
	echo count($arrayParamA);
}

function funcB($arrayParamB)
{
	funcA($arrayParamB);
	foreach($arrayParamB as $elem)
		echo ' - ', $elem;
}


$arr = array(1, 2, 3, 4, 5);
funcB($arr);
?>
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Post by webcan »

Thank you for your reply, but I think there is a big difference with the code that I am using, and that is that the $row array is declared within the first function, not outside of that function.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

doesn't make a difference
as you can see funcB passes $arrayParamB to funcA. And that isn't declared outside.
ok, here's the light version of the example ;)

Code: Select all

<?php
function funcA($arrayParamA)
{
	echo count($arrayParamA);
}

function funcB()
{
	$arr = array(1, 2, 3, 4, 5);
	funcA($arr);
}

funcB();
?>
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Post by webcan »

OK, I see what you mean.

But now, :-), I want to be able to do this:

Code: Select all

<?php 
function funcA($arrayParamA) 
{ 
   echo count($arrayParamA); 
} 

function funcB() 
{ 
   $arr = array(1, 2, 3, 4, 5); 
} 

funcB(); 
funcA($arr);
?>
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

btw: this way any changes on the array in funcA will only affect the copy within funcA

Code: Select all

<?php
function funcA($arrayParamA)
{
	$arrayParamA[] = 6;
	echo count($arrayParamA);
}

function funcB()
{
	$arr = array(1, 2, 3, 4, 5);
	funcA($arr);
	print_r($arr);
}

funcB();
?>
If funcA has to change the array and it must be visible outside pass a reference

Code: Select all

<?php
function funcA(&$arrayParamA)
{
	$arrayParamA[] = 6;
	echo count($arrayParamA);
}

function funcB()
{
	$arr = array(1, 2, 3, 4, 5);
	funcA($arr);
	print_r($arr);
}

funcB();
?>
only difference is the & in front of $arrayParamA
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

simple answer to your last question

Code: Select all

<?php
function funcA($arrayParamA)
{
  echo count($arrayParamA);
}

function funcB()
{
  $arr = array(1, 2, 3, 4, 5);
  return $arr;
}

funcA( funcB() );
?>
;)
arrays cannot only be parameters but also return values
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Post by webcan »

OK, I think I am almost at the answer to my question...... :) (btw you are awesome ;-) )

If I do funcA( funcB() ); is that not going to execute funcB() at that time?

Basically, I want to run funcA(), which will create the $row array, and populate it, and then further down in the code, I want to run funcB() which will refer to the $row array from funcA() without running funcB() again.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

yes, it was only a shourtcut

Code: Select all

<?php
function printRowArray($arr)
{
	foreach($arr as $key=>$value)
		echo $key, '=>', $value, "<br />\n";
}

function getRowAsArray()
{
	return array(1, 2, 3, 4, 5);
}

$row = getRowAsArray();
// <- something else here, maybe something like
$row['the'] = 'end';
// ->

printRowArray($row);
?>
---
but to answer your first question (now that there's hopefully no need anymore)

Code: Select all

<?php
function funcA()
{
	global $myArray;
  echo count($myArray), ' - ', count($GLOBALS['gArray']);
}

function funcB()
{
	global $myArray; // either mark it as global
  $myArray = array(1, 2, 3, 4, 5);
  
  $GLOBALS['gArray'] = array(1, 2, 3, 4); // or use this superglobal array
}

funcB();
funcA();
?>
But try to avoid globals if it can be done as return value. This might be a personal preference but global variables are what I'd call side-effects...
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Post by webcan »

OK, I will try this.

Thank you!!
webcan
Forum Commoner
Posts: 66
Joined: Tue Oct 28, 2003 2:25 pm
Location: Toronto, Canada

Post by webcan »

It worked!

Danke sehr.

PS. I think you should post a reply so that I can be your 4,000th post ;-)
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

oops, I've already wasted that opportunity ;)
Post Reply