Click to See Complete Forum and Search --> : Problems using global variables


wk4997
01-07-2004, 09:19 AM
function makeMe($myvar){

global $$myvar;
$$myvar = array();
$$myvar[0] = "main/main69.jpg";
echo $$myvar[0];
return $$myvar;
}

makeMe('main');
echo $$myvar[0] . "<BR>\n";
echo $myvar[0] . "<BR>\n";
echo "HERE 2!<BR>\n";



Now, with the above function, I can get the first echo to work, but when I run the function the next two echos don't work and the last done appears.

This output can be seen at the following link:
http://apo.rose-hulman.edu/pictures/rdpicdir2.php

Why is this happening, since I'm trying a similar method to generate an array dynamically to make a thumbnail page.

Thanks,
Wes K. :)

Khalid Ali
01-07-2004, 05:18 PM
its seems like both echo statements are working

wk4997
01-07-2004, 06:45 PM
Then, why doesn't the two echo statements, after the makeMe() function is run actually output anything useful, like what I had with the first statement? Shouldn't the global variable act "global" instead of "local" as this behavior suggests?

Wes K. :confused:

Khalid Ali
01-07-2004, 07:14 PM
I amstill not sure what are you trying to do but here is the code where I have decalred a global array and then used this global array in the function adn then out side of the function

$myvar = array();

function makeMe($myvar){
global $myvar;
$myvar = array();
$myvar[0] = "main/main69.jpg";
echo $myvar[0];
return $myvar;
}

makeMe('main');
echo $myvar[0] . "<BR>\n";
echo $myvar[0] . "<BR>\n";

echo "HERE 2!<BR>\n";

wk4997
01-07-2004, 07:23 PM
Why are you declaring this at the beginning of the code?

$myvar = array();


I'm trying to create a variable from whatever name I pass it?

For example:

makeMe('main');
// should return a variable called $main
// that is an array that's global as well
// that has only one value


- Wes

Kr|Z
01-08-2004, 06:54 AM
Is it something like this you want?


function makeMe($myvar) {
eval("global \$".$myvar.";\n");
eval("\$".$myvar."[0] = \"main/main69.jpg\";\n");
}

makeMe('main'); // Returns a variable called $main
echo $main[0] . "<hr>";

makeMe('hello'); // Returns a variable called $hello
echo $hello[0];

wk4997
01-14-2004, 01:40 AM
function getPicFilenames($myvar){

$tempname = $myvar;
eval("global \$".$myvar.";\n");
eval("\$".$myvar."=array();");
eval("global \$".$maxindex.";\n"); // I need a $maxindex for running loops
// but this won't allow me to run it - don't know why

...


Whenever I try to have 2 global variables, the following output is produced:


Parse error: parse error, expecting `T_VARIABLE' or `'{'' or `'$'' in /home/httpd/html/pictures/rdpicdir2.php(8) : eval()'d code on line 1



PLEASE HELP!
Thanks,
Wes K :)

P.S. If you want to see the current output, it's at: http://apo.rose-hulman.edu/pictures/rdpicdir2.php