SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: variable scope

  1. #1
    SitePoint Zealot jsk137's Avatar
    Join Date
    Jan 2002
    Posts
    151
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    variable scope

    How do I make a variable available outside its current scope? I have a variable called $balance and I need to use it outside its current scope.

    I have tried to define a constant, but I only get '$balance' as its value. I need to assign the value of the variable $balance to the constant I am defining.

    Thanks,
    Jeff

  2. #2
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Perth, Australia
    Posts
    157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    By scope, I assume it is in a function/class. You could define the variable initally outside the function/class, and use the global statement at the start of the function like this:

    PHP Code:
    $balance "";

    function 
    foo($arg) {
      global 
    $balance;
      
    $balance $arg;
      return 
    $arg;
    }

    $test foo("test");
    echo 
    $test $balance
    Paul Davey
    webmaster for Whitford Church of Christ


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
  •