Jump to content

Don't quite get Returning Values By Value script


co.ador

Recommended Posts

function get_global_variable_value($name)
{
return $GLOBALS[$name];
}
$num = 10;
$value = get_global_variable_value("num");
print $value;

 

The function above uses the same function name for two different purposes and getting different values only by assigning a different argument that probably will have a different value than the original function argument above.

 

the above script prints 10 how is that?

 

 

Link to comment
Share on other sites

Ok, pardon me if this sounds wrong but...

 

It looks like what you are doing is a horrible idea.  I'm not 100% sure what you're trying to do, but it looks like a horrible idea. 

 

Are you trying to reference a variables value by it's name? 

 

 

Link to comment
Share on other sites

function get_global_variable_value($name)
{
return $GLOBALS[$name];
}
$num = 10;
$value = get_global_variable_value("num");
print $value;

 

the above script prints 10 how is that?

 

$num has a value of 10 within global scope.  $GLOBALS is an array of all global scope variables.  $GLOBALS['num'] is the same as accessing $num as though it was in scope.  As always, check the manual if you're confused about a part of the language: globals

 

With all that said, don't use the 'global' keyword or the $GLOBALS array.  It leads to poor code.

Link to comment
Share on other sites

function get_global_variable_value($name)
{
return $GLOBALS[$name];
}
$num = 10;
$value = get_global_variable_value("num");
print $value;

 

the above script prints 10 how is that?

 

$num has a value of 10 within global scope.  $GLOBALS is an array of all global scope variables.  $GLOBALS['num'] is the same as accessing $num as though it was in scope.  As always, check the manual if you're confused about a part of the language: globals

 

With all that said, don't use the 'global' keyword or the $GLOBALS array.  It leads to poor code.

 

Second that. I used to have a bad habit of declaring global variables when inside a function if I didn't want to have to pass those variables directly. It's just a bad idea.

 

..and writing a function to return a global variable (I would have thought) was pointless as you can get the global variable any way with $GLOBALS['num'].

Link to comment
Share on other sites

Edit: Nightslyr beat me to it, but I'll post this anyway:

 

function get_global_variable_value($name)
{
return $GLOBALS[$name];
}
$num = 10;
$value = get_global_variable_value("num");
print $value;

 

The function above uses the same function name for two different purposes and getting different values only by assigning a different argument that probably will have a different value than the original function argument above.

 

the above script prints 10 how is that?

 

 

This is all about variable scope. The process above is pretty strait-forward. When you set $num = 10 it is done outside of any function so it has "global" scope. When you are then IN a function that variable is not directly available. FOr example, if you defined $num inside a function it is a different instance of $num. However, you can reference global variables in a couple of ways.

 

1. Initiate the gloabl variable within the function

function name()
{
    global $num;
}

In that case $num inside the function is the same variable as $num outside the function

 

2. Using $GLOBALS keyword as in the script above. It is an associate array of all the gloabl variables. So, since $num was defined outside the function it is gloabl and you can reference it inside a function, any function, using $GLOBALS['num']

 

See the manual here: http://php.net/manual/en/reserved.variables.globals.php

 

Although I agree that function looks to be a bad idea, but is constructive for understanding the behavior

Link to comment
Share on other sites

I think it's just a constructive to see how that will work.

 

was not clear about.

 

To be totally clear global scope means that any variable defined outside of a function can be accessed from any part of the script right, but not from inside a function. In order to bring a variable in the global scope inside of a function would you used the global, or $Globals variables to suck it in let say....

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.