mblase
asked on
Why isn't this global?
Here's the code outside of my PHP functions:
$bgcolor1 = "#000000";
$bgcolor2 = "#FFFF00";
$bgcolor3 = "#CCCC00";
$bgcolor4 = "#FFFF00";
$textcolor1 = "#FFFFFF";
$textcolor2 = "#FFFF00";
$bgimage1 = "themes/CCore/images/barra .gif";
Inside of a function, I localize them like so:
global $bgcolor1, $bgcolor2, $bgcolor3, $bgcolor4, $textcolor1, $textcolor2, $bgimage1;
All of those variables localize successfully, EXCEPT for $bgimage1. No matter what I rename the variable to, it's still gets returned as an empty string when I call it inside a function, but as the correct string from outside the function.
Am I missing something obvious here?
$bgcolor1 = "#000000";
$bgcolor2 = "#FFFF00";
$bgcolor3 = "#CCCC00";
$bgcolor4 = "#FFFF00";
$textcolor1 = "#FFFFFF";
$textcolor2 = "#FFFF00";
$bgimage1 = "themes/CCore/images/barra
Inside of a function, I localize them like so:
global $bgcolor1, $bgcolor2, $bgcolor3, $bgcolor4, $textcolor1, $textcolor2, $bgimage1;
All of those variables localize successfully, EXCEPT for $bgimage1. No matter what I rename the variable to, it's still gets returned as an empty string when I call it inside a function, but as the correct string from outside the function.
Am I missing something obvious here?
ASKER
My actual code looks like this:
<?php
$bgcolor1 = "#000000";
$bgcolor2 = "#FFFF00";
$bgcolor3 = "#CCCC00";
$bgcolor4 = "#FFFF00";
$textcolor1 = "#FFFFFF";
$textcolor2 = "#FFFF00";
$bgimage1 = "themes/CCore/images/barra .gif";
function themeindex (...) {
global $bgcolor1, $bgcolor2, $bgcolor3, $bgcolor4, $textcolor1, $textcolor2, $bgimage1;
echo "$bgimage1";
...
and that last line echoes an empty string.
<?php
$bgcolor1 = "#000000";
$bgcolor2 = "#FFFF00";
$bgcolor3 = "#CCCC00";
$bgcolor4 = "#FFFF00";
$textcolor1 = "#FFFFFF";
$textcolor2 = "#FFFF00";
$bgimage1 = "themes/CCore/images/barra
function themeindex (...) {
global $bgcolor1, $bgcolor2, $bgcolor3, $bgcolor4, $textcolor1, $textcolor2, $bgimage1;
echo "$bgimage1";
...
and that last line echoes an empty string.
Replace
echo "$bgimage1";
with
echo $GLOBALS["bgimage1"];
and tell me if that makes any difference.
What parameters are you passing to the function?
Ant
echo "$bgimage1";
with
echo $GLOBALS["bgimage1"];
and tell me if that makes any difference.
What parameters are you passing to the function?
Ant
ASKER
Using $GLOBALS made no difference. None of the function parameters have the same variable name.
What about if you move $bgimage1 from the end to somewhere else in the global statement? Does it work then?
What version of PHP are you using? e.g. 4.0.5
Ant
What version of PHP are you using? e.g. 4.0.5
Ant
ASKER
Here's an odder bit: if I do the following:
$bgimage1 = "themes/CCore/images/barra .gif";
...
function themesidebox($title, $content) {
global $bgcolor1, $bgcolor2, $bgcolor3, $bgcolor4, $textcolor1, $textcolor2, $bgimage1;
echo "bgimage1=$bgimage1";
$bgimage1 = "themes/CCore/images/barra .gif";
...then $bgimage1 appears blank when the echo is first displayed, but is correctly set by the next line and the echo displays the image URL every time thereafter.
$bgimage1 = "themes/CCore/images/barra
...
function themesidebox($title, $content) {
global $bgcolor1, $bgcolor2, $bgcolor3, $bgcolor4, $textcolor1, $textcolor2, $bgimage1;
echo "bgimage1=$bgimage1";
$bgimage1 = "themes/CCore/images/barra
...then $bgimage1 appears blank when the echo is first displayed, but is correctly set by the next line and the echo displays the image URL every time thereafter.
It does seem like a bug....which is why I asked the last lot of questions that I did.....any response to them? :o)
Ant
Ant
To me it sounds like either that variable has been
reserved by PHP in some strange way or a plain bug.
I ran the following code on 2 machines:
<?php
$bgcolor1 = "#000000";
$bgcolor2 = "#FFFF00";
$bgcolor3 = "#CCCC00";
$bgcolor4 = "#FFFF00";
$textcolor1 = "#FFFFFF";
$textcolor2 = "#FFFF00";
$bgimage1 = "themes/CCore/images/barra .gif";
function themeindex () {
global $bgcolor1,$bgcolor2,$bgcol or3,$bgcol or4,$textc olor1,$tex tcolor2, $bgimage1;
echo $bgimage1."<br>".$bgcolor1 ;
}
themeindex();
echo phpinfo();
?>
..and the result as follows:
WIN PHP 4.0.5 (apache) OUTPUT:
themes/CCore/images/barra. gif
#000000
LINUX PHP 4.0.4pl1 OUTPUT:
themes/CCore/images/barra. gif
#000000
Why not just rename that bloody variable? :)
/Regards Spiffen.
reserved by PHP in some strange way or a plain bug.
I ran the following code on 2 machines:
<?php
$bgcolor1 = "#000000";
$bgcolor2 = "#FFFF00";
$bgcolor3 = "#CCCC00";
$bgcolor4 = "#FFFF00";
$textcolor1 = "#FFFFFF";
$textcolor2 = "#FFFF00";
$bgimage1 = "themes/CCore/images/barra
function themeindex () {
global $bgcolor1,$bgcolor2,$bgcol
echo $bgimage1."<br>".$bgcolor1
}
themeindex();
echo phpinfo();
?>
..and the result as follows:
WIN PHP 4.0.5 (apache) OUTPUT:
themes/CCore/images/barra.
#000000
LINUX PHP 4.0.4pl1 OUTPUT:
themes/CCore/images/barra.
#000000
Why not just rename that bloody variable? :)
/Regards Spiffen.
ASKER
I've renamed the variable once before ($bgbar1 to $bgimage1), with no change.
Not sure what version of PHP it is; I'm not running it on my own machine.
Not sure what version of PHP it is; I'm not running it on my own machine.
Have you tried moving it from the end of the list like I suggested and seeing if that makes any difference?
Ant
Ant
You'll see the PHP version if you echo phpinfo();
<?php
echo phpinfo();
?>
<?php
echo phpinfo();
?>
Spiffen,
Just as a note, I never need to use echo for phpinfo().
If I do, I get an extra "1" at the bottom of the page (which I suppose represents True as the function worked OK!).
Just as a note, I never need to use echo for phpinfo().
If I do, I get an extra "1" at the bottom of the page (which I suppose represents True as the function worked OK!).
ASKER
Of course. :-) v.4.0.0, it sez
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I agree with the previous comments that this sounds like an early PHP 4 bug.
I have found it useful to sometime include the following snipit in my function to get "C" like globals.
// import globals
foreach (array_keys($GLOBALS) as $key) {
if(!isset(${$key})) global ${$key};
}
I have found it useful to sometime include the following snipit in my function to get "C" like globals.
// import globals
foreach (array_keys($GLOBALS) as $key) {
if(!isset(${$key})) global ${$key};
}
:o)
Ant