Page 1 of 1

How do I declare an array as global?

Posted: Wed Oct 29, 2003 8:17 am
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?

Posted: Wed Oct 29, 2003 8:22 am
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);
?>

Posted: Wed Oct 29, 2003 8:30 am
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.

Posted: Wed Oct 29, 2003 8:34 am
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();
?>

Posted: Wed Oct 29, 2003 8:38 am
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);
?>

Posted: Wed Oct 29, 2003 8:38 am
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

Posted: Wed Oct 29, 2003 8:40 am
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

Posted: Wed Oct 29, 2003 8:45 am
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.

Posted: Wed Oct 29, 2003 8:51 am
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...

Posted: Wed Oct 29, 2003 8:53 am
by webcan
OK, I will try this.

Thank you!!

Posted: Wed Oct 29, 2003 8:58 am
by webcan
It worked!

Danke sehr.

PS. I think you should post a reply so that I can be your 4,000th post ;-)

Posted: Wed Oct 29, 2003 9:12 am
by volka
oops, I've already wasted that opportunity ;)