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 06-08-2008, 12:06 AM   #1 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default A little unclear about "global" keyword...

I'm a little confused about the "global" keyword (not the $GLOBALS keyword).

Example in "main" part of a PHP script:

global $sun ;
$sun = "star" ;

I think the following statement is true, but is it?

If I write a user-defined function, I must again name the var $sun to be a global within the UDF in order for it to be available to use in the function (unless I pass it to the UDF as a parameter).

Thanks,
Dave
Dave is offline  
Reply With Quote
Old 06-08-2008, 12:23 AM   #2 (permalink)
Wizard
Top Contributor 
 
Village Idiot's Avatar
 
Join Date: Sep 2007
Posts: 1,299
Thanks: 17
Village Idiot is on a distinguished road
Default

It has to do with variable scopes. Look at the following code
PHP Code:
<?php
function seta()
{
    
$a "Hello There";
}

seta();
echo 
$a;
?>
That will output nothing because $a is not in a scope for the code outside the UDF to access. Now look at this code
PHP Code:
<?php
function seta()
{
    global 
$a "Hello There";
}

seta();
echo 
$a;
?>
That code will echo the value that $a was assigned to in the function.
__________________

Village Idiot is offline  
Reply With Quote
The Following 3 Users Say Thank You to Village Idiot For This Useful Post:
Dave (06-08-2008), ETbyrne (06-08-2008), Orc (06-11-2008)
Old 06-08-2008, 11:34 AM   #3 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

Right, thanks, but my question was asking if this is true or if this is false:

"If I write a user-defined function, I must again name the var $sun to be a global within the UDF in order for it to be available to use in the function (unless I pass it to the UDF as a parameter)."

If it is TRUE, what is the rationale of having to "rename" $sun as a global within the context of a UDF?

Thanks again,
Dave
Dave is offline  
Reply With Quote
Old 06-08-2008, 01:52 PM   #4 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

It's not called renaming, it's simply a statement to help the parser understand what is the variable that needs to be modified (is it coming from the namespace or is it coming from the current function -- the latter is assumed by default).

PHP Code:
$a 5;

function 
showA()
{
    global 
$a// not renaming anything, just letting the parser know that the $a is declared globally, and use its value, instead of defining a function-level variable called $a

    
$a 10;
}

showA();
echo 
$a// will output 10, but if you remove the global statement (or comment it), it will output 5, of course 
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
Dave (06-08-2008)
Old 06-08-2008, 03:44 PM   #5 (permalink)
The Acquainted
 
Dave's Avatar
 
Join Date: Apr 2008
Posts: 110
Thanks: 97
Dave is on a distinguished road
Default

Thanks, although... I'm still not asking the question correctly, I guess, which again proves that I'm far from the sharpest tool in the shed.

I'll try again later after I've put some more study in this.

Appreciatively,

Dave
Dave is offline  
Reply With Quote
Old 06-08-2008, 04:42 PM   #6 (permalink)
is cute and cuddly
 
delayedinsanity's Avatar
 
Join Date: Mar 2008
Location: Vegas, Baby
Posts: 963
Thanks: 31
delayedinsanity is on a distinguished road
Default

If I get you right, you're asking why, if you've already declared a variable as global once in a script, would you have to redeclare it as global anywhere else, as in, once you've stated it's a global, how come the entire script just doesn't immediately recognize it as a global? I *think* this may be explained by how the global keyword is working.

Technically all variables defined outside of a function are considered 'global'. They can be used anywhere within the script, including from within a function so long as you access them via the $GLOBALS array or with the global keyword. This is because functions are almost like a private namespace, where anything you do inside of them is seperate from the global scope of the script. So when you do;

PHP Code:
function globalVars() {
    global 
$szString;
    echo 
$szString;

    
//or

    
echo $GLOBALS['szString'];

...you're not actually renaming or redeclaring $szString as a global, you're just telling the function itself to use the global version of $szString as opposed to it's own local copy. Thus why you have to do it in every UDF you're using it in, because you're telling those specific functions to use the global copy instead of their own copy.

Was that even remotely closer to what you were looking for?
-m
delayedinsanity is offline  
Reply With Quote
The Following User Says Thank You to delayedinsanity For This Useful Post:
Dave (06-10-2008)
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


All times are GMT. The time now is 06:53 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