Jump to content


Photo

Globals and Valid Use...


  • Please log in to reply
2 replies to this topic

#1 mem0ri

mem0ri
  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts

Posted 06 March 2006 - 05:11 PM


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
}

#2 Gaia

Gaia
  • Members
  • PipPipPip
  • Advanced Member
  • 95 posts
  • LocationCanada

Posted 06 March 2006 - 05:18 PM

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.

#3 mem0ri

mem0ri
  • Members
  • PipPipPip
  • Advanced Member
  • 110 posts

Posted 06 March 2006 - 05:22 PM

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?




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users