Jump to content

Archived

This topic is now archived and is closed to further replies.

mem0ri

Globals and Valid Use...

Recommended Posts


The question is purely academic at this point...but I have a question about the valid use of a global variable and whether the following would work or error out:


$x = 1;


function myFunction($y = $x)
{
global $x;

do stuff
}

Share this post


Link to post
Share on other sites
Yes, that would work. That's basically what it is used for, using variables that were initiated outside of a function, inside a function.

If that makes any sense :p

I'm sure someone else can add some more to my try at answering your question.

Share this post


Link to post
Share on other sites
Right...my big question is on the: function myFunction($y = $x) part...

...because the global hasn't been declared within the function yet...and is operating as the default value of information passed into the variable...


...additional...slightly related question...let's say I have:

function myFunction($y = $x, $b = "string", $n = 1)

...and I want to call the function with somethin' like a myFunction("string")...where it will only change the default value of $b...leaving $y and $n alone...

...basically...is there a way to explicitly address to which value a function call sends its arguments?

Share this post


Link to post
Share on other sites

×

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.