Solved

Why isn't this global?

Posted on 2001-06-20
15
283 Views
Last Modified: 2006-11-17
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?
0
Comment
Question by:mblase
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 2
  • +2
15 Comments
 
LVL 15

Expert Comment

by:a.marsh
ID: 6212348
I feel as though your are getting confused....can we see the actual code where you are using this...

:o)

Ant
0
 
LVL 2

Author Comment

by:mblase
ID: 6212382
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.
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6212408
Replace

echo "$bgimage1";

with

echo $GLOBALS["bgimage1"];

and tell me if that makes any difference.

What parameters are you passing to the function?

Ant
0
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
LVL 2

Author Comment

by:mblase
ID: 6212416
Using $GLOBALS made no difference. None of the function parameters have the same variable name.
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6212429
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
0
 
LVL 2

Author Comment

by:mblase
ID: 6212452
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.
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6212459
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
0
 

Expert Comment

by:spiffen
ID: 6213847
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,$bgcolor3,$bgcolor4,$textcolor1,$textcolor2, $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.
0
 
LVL 2

Author Comment

by:mblase
ID: 6214326
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.
0
 
LVL 15

Expert Comment

by:a.marsh
ID: 6214341
Have you tried moving it from the end of the list like I suggested and seeing if that makes any difference?

Ant
0
 

Expert Comment

by:spiffen
ID: 6214983
You'll see the PHP version if you echo phpinfo();
<?php
 echo phpinfo();
?>
0
 
LVL 40

Expert Comment

by:Richard Quadling
ID: 6215059
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!).
0
 
LVL 2

Author Comment

by:mblase
ID: 6215145
Of course. :-)  v.4.0.0, it sez
0
 
LVL 15

Accepted Solution

by:
a.marsh earned 100 total points
ID: 6215179
I would highly recommend upgrading - I genuinely believe this is a bug - if you look through the PHP bugs database there are bugs relating to global in the early releases of PHP4.

Either that or don't use global - there are alternatives depending on what you want to do e.g. passing values by reference.

:o)

Ant
0
 
LVL 1

Expert Comment

by:sweatje
ID: 6240105
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};
     }
0

Featured Post

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question