SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    setting global variable... how?

    How can I set a global variable..

    say I have a variable within a if-statement...

    if (isset ($yipie)){

    $new = do this

    $id = $new
    }

    and I want to use the $id OUTSIDE this variable, how do I do this?

  2. #2
    SitePoint Addict Percipient's Avatar
    Join Date
    Jun 2000
    Location
    Summerland, BC, Canada.
    Posts
    226
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Variables defined outside of an 'if' statement are available inside the 'if' statement, or 'while', or 'switch', or 'for'. The only place variables have a local scope is inside of a user defined function, like this...
    PHP Code:
    function doSomething () {
     
    $variable "Okay, I did something. Now what?";
     return 
    $variable;

    Any variable defined in a user function will be ignored outside that function. Likewise, any variable defined outside the function (even built-in variables like $PHP_SELF!), will not be available inside. The way to get around this is to put this line at the top of your function...
    PHP Code:
    global $var1$var2$etc
    ... replacing my example variable names with the ones you want available globally.

    Hope that's of some help to you.
    Ed Rands

  3. #3
    SitePoint Evangelist jazztie's Avatar
    Join Date
    Mar 2001
    Location
    the Netherlands
    Posts
    519
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanx... I'll try it first thing tomorrow!
    Now, back to my beer.

    Jazz

  4. #4
    SitePoint Guru
    Join Date
    Jan 2001
    Location
    Alkmaar, Netherlands
    Posts
    710
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I felt you are from holland when you said beer


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •