Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Global Variables 1

Status
Not open for further replies.

Michael42

Programmer
Joined
Oct 8, 2001
Messages
1,454
Location
US
I am learning PHP 4 and trying to do things by the book. As such it seems it is recommended to use the default globals setting of register_globals = Off .

My brain hurts from trying to intialize and then later in my application use my global variables.

I have been trying variations of this without success:
// Init Global Variables
$AppColor1 =&$GLOBALS["#012C95"];

...

<body link=&quot;<?=$GLOBALS[&quot;AppColor1&quot;]?>&quot; >:


Is anyone using Globals the &quot;new&quot; way. I do not even know the old. Please help me.

Thanks,

Michael42




 
You have several issues at once. I really have no idea what you're trying to do.

But what's wrong with saying:

$GLOBALS['AppColor1'] = '#012C95';
.
.
.
print '<body link=&quot;'. $GLOBALS['AppColor1'] . '&quot;>';

or even:

$AppColor1 = '#012C95';
.
.
.
print '<body link=&quot;'. $AppColor1 . '&quot;>';


register_globals has only to do with whether values from HTML inputs (POST- and GET-method form submissions, cookies, etc.) will be instantiated as individual variables in the global variable scope.

As an example, if you have a form:
<form method=&quot;post&quot; action=foo.php&quot;>
<input type=text name=foo>
<input type=submit>
</form>

When that form is submitted to foo.php, if register_globals is set to &quot;off&quot;, you will have the value of the form element &quot;foo&quot; available to you only in $_POST['foo']. With register_globals set to &quot;on&quot;, it will be available both in $_POST['foo'] and in $foo at the global scope.

The global variable scope being the one you're in when your script starts, before your program flow enters any user-defined functions or classes. There exist superglobal arrays, of which $GLOBALS is one, which are available everywhere, including inside functions and classes.

(It is recommended that you leave register_globals set of &quot;off&quot; and reference these variables via their superglobal array indeces because:[ul][li]this can prevent variable &quot;poisoning&quot; by spurious inputs[/li][li]superglobal arrays are available everywhere[/li][li]the superglobal arrays sort of self-document your code[/li][/ul]

In terms of regular variable use, the only time use of $GLOBALS becomes necessary is when you are using functions or classes. The variables you instantiate in the main scope are not available in functions or classes unless you pull them into the function or class through the use of the &quot;global&quot; operator. But $GLOBALS is available everywhere.

One gotcha: $GLOBALS contains everything that exists at the global scope, including itself -- some versions of PHP will get into an infinite loop when you perform print_r ($GLOBALS).


Want the best answers? Ask the best questions: TANSTAAFL!
 
The new way is just like so...
Code:
$GLOBALS[&quot;AppColor1&quot;] = &quot;#012C95&quot;;

...

<body link=&quot;<?=$GLOBALS[&quot;AppColor1&quot;];?>

$GLOBALS[] is an array, you put put whatever you'd like as the index of the array (in this case, AppColor1) and whatever you want as the value of that index (in this case, #012C95).

-Rob

 
<mutter> sorry sleipnir, my post got caught up in local network traffic for a long time.

-Rob
 
sleipnir214/skiflyer:

Can I use this $GLOBAL thing with two pages? I have a page that does a lot of queries to the database which I can categorize into smaller parts. I want to seperate them in to different pages and just <?include?> them on the main page.

I tried this before without $GLOBAL and obviously it didn't work . The display could not recognize the variables in the other pages.

Is there any other way? or should I use $GLOBAL?

I don't really like building classes. It complicate things for me. (not a good OO programmer)

Regards,

Namida
 
I get the impression that you aren't grokking $GLOBALS. $GLOBALS is not a static entity that is stored somewhere -- it is a dynamically created array that exists in memory for only as long as your script runs. Once script execution stops, $GLOBALS ceases to exist.

I recommend you take a look at the PHP online manual under variable scoping ( for a better explanation of variable scoping in PHP than I could write. This has some very good examples of the use of $GLOBALS.


Want the best answers? Ask the best questions: TANSTAAFL!
 
All your suggestions were great (and useful).

What I needed to understand was using it like this (via skiflyer):

$GLOBALS[&quot;AppColor1&quot;] = &quot;#012C95&quot;;


Thanks!!!

 
This line (from skiflyer):

$GLOBALS[&quot;AppColor1&quot;] = &quot;#012C95&quot;;

and this line:

$AppColor1 = &quot;#012C95&quot;;

are actually functionally equivalent, if and only if neither line appears inside a function or class. In the global scope, use of $GLOBALS['AppColor1'] is unnecessary -- referencing $AppColor1 is exactly the same thing and easier to type.

Want the best answers? Ask the best questions: TANSTAAFL!
 
Thanks. I see that I am totally off there.

So is there any way to reuse the variable from an included page? Or should it be the normal $varname access method.

I tried but it didn't show up on the echo so I thought it wasn't recognized.

eg. File a.php
<?
$a = 4;
?>
File b.php

<?
include(&quot;a.php&quot;);
echo $a;
?>

How do I make it work? Do I have to store it in a session variable?

Regards,

Namida
 
When included code is executed, that code inherits the variable scope of the point in the calling script where the include statement appears. Explicit manipulation of $GLOBALS is not necessary unless you're doing some things with functions.

And the code you've posted works for me when I point my browser to b.php.

Want the best answers? Ask the best questions: TANSTAAFL!
 
Then I must be doing something wrong. Thanks! :)Actually a would be a result from the database but since a=4 works i think it should work as well Thanks!

Regards,

Namida
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top