TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 05-12-2009, 06:25 PM   #1 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default global variables or not?

Ok so I'm reading a book called PHP 5 Power Programming.


Quote:
PHP does not support global variables like many other programming
languages (except for some special pre-defined variables, which we discuss
later).
He then goes on to say you can access main script variables like so:

$GLOBALS['variable_here'];


I think we all know that. So when someone asks about accessing PHP's global variables, the technical correct answer is NO, PHP does not support it but here's a way to access it within a function....

Is that correct? Kinda confusing....but I get it...


Just want to make sure, because when I take the test, I don't want them asking me, "Does PHP support global variables?"

Then I say, "No".

And the test comes back, "WRONG, you can access it this way: $GLOBALS['variable_here'].."

Which then would mean the test is wrong...
allworknoplay is offline  
Reply With Quote
Old 05-12-2009, 06:54 PM   #2 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Oh I'd like to add, later on when he talks about SUPER GLOBAL ARRAYS, he says this about "global variables..."


Quote:
You don’t have to use the $GLOBALS[] array, which
allows for accessing global variables without having to predeclare them or
using the deprecated globals keyword.

So it sounds like he's stepping ontop of himself. If PHP doesn't support global variables, why is he saying that?

Should he be better off saying, something like:

You don't have to use the $GLOBALS[] array, which allows for accessing your main scripts variables.....
allworknoplay is offline  
Reply With Quote
Old 05-12-2009, 08:30 PM   #3 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

Generally in programming, global variables are those which are accessible in every scope. So when the author says that PHP does not support them, it is true. Take care not to confuse "global variables" with "variables in the global scope": when talking about global variables in PHP that distinction is often blurred.

Global variables (are not supported)
PHP Code:
$global 'my global variable';
function 
test()
{
  echo 
$global;
}
test(); // Notice:  Undefined variable: global ... 
The quote in the second post is referring only to the superglobal variables. To put it into context, a fuller quote is:
On a language level, it is important to know that you can access these [superglobal] variables anywhere in your script whether function, method, or global scope. You don’t have to use the $GLOBALS[] array, which allows for accessing global variables without having to predeclare them or using the deprecated globals keyword.
Salathe is offline  
Reply With Quote
Old 05-12-2009, 08:42 PM   #4 (permalink)
The Gregarious
 
allworknoplay's Avatar
 
Join Date: Feb 2009
Location: New York
Posts: 645
Thanks: 64
allworknoplay is on a distinguished road
Default

Quote:
Originally Posted by Salathe View Post
Generally in programming, global variables are those which are accessible in every scope. So when the author says that PHP does not support them, it is true. Take care not to confuse "global variables" with "variables in the global scope": when talking about global variables in PHP that distinction is often blurred.

Global variables (are not supported)
PHP Code:
$global 'my global variable';
function 
test()
{
  echo 
$global;
}
test(); // Notice:  Undefined variable: global ... 
The quote in the second post is referring only to the superglobal variables. To put it into context, a fuller quote is:
On a language level, it is important to know that you can access these [superglobal] variables anywhere in your script whether function, method, or global scope. You don’t have to use the $GLOBALS[] array, which allows for accessing global variables without having to predeclare them or using the deprecated globals keyword.
Fantastic....understand now.....

I will be looking forward to your book:

Mastering PHP by Salathe

allworknoplay is offline  
Reply With Quote
Old 05-13-2009, 10:20 AM   #5 (permalink)
The Wanderer
 
foobarph's Avatar
 
Join Date: May 2009
Posts: 23
Thanks: 4
foobarph is on a distinguished road
Default

is it also right to say that GLOBAL variables can be set to true/false in the ini file?
foobarph is offline  
Reply With Quote
Old 05-13-2009, 10:53 AM   #6 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

I'm not sure if this is what you are asking foobarph but there is an ini directive called register_globals which determines whether or not to register the EGPCS array variables (those within $_ENV, $_GET, $_POST, $_COOKIE and $_SERVER) as normal, global-scope variables (e.g. $_GET['foo'] being available as $foo). This feature is deprecated as of PHP 5.3.0 and removed as of PHP 6.0.0.

Quote:
Mastering PHP by Salathe
Haha.
Salathe is offline  
Reply With Quote
Old 05-25-2009, 06:07 AM   #7 (permalink)
The Contributor
 
Join Date: Feb 2007
Posts: 64
Thanks: 9
Killswitch is on a distinguished road
Default

To understand what he is talking about, try looking and studying C++. Variables are much the same, but their scope is very different. I'm not too far into learning the lang myself, but from what I've gathered, in C++, variables can be set to the scope of a block of code ( not just a function, a block of code, such as a loop ). Global variables are those started outside of any code blocks ( again, I am pretty sure about this but might have read incorrectly ).

In Php, you can start a variable inside or outside of a block of code and it does not become global. It only becomes global once you implicitly assign it as global.
Killswitch is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Defining your World: All About Constants Wildhoney General 15 01-29-2013 12:32 PM
Is there a function to list all "active" variables Dave Absolute Beginners 3 06-05-2008 04:03 AM
assigning pcre replacements ( $1 or $2 or whatever ) to variables Orc General 7 04-21-2008 07:53 PM
When was the last time you used the global construct? Wildhoney The Lounge 11 01-28-2008 08:41 PM
Variables in Regular Expession Wildhoney Advanced PHP Programming 4 12-06-2007 03:44 PM


All times are GMT. The time now is 10:32 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design