SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    263
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Variable scope and function inside function

    Please consider the following scenario

    PHP Code:
    $a 'hi';
    one();

    function 
    one() {
    global 
    $a;

    $b 'hi';
    echo 
    $a;
    echo 
    '-';
    echo 
    two();
    }

    function 
    two() {
    global 
    $b;
    echo 
    $b;

    This will produce
    hi
    instead of
    hi-hi
    What is the workaround of such a limitation if i can call it?

  2. #2
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,506
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    don't use globals, send parameters instead:
    PHP Code:
    $a 'hi';
    one($a);

    function 
    one($a) {
      
    $b 'hi';
      echo 
    $a;
      echo 
    '-';
      
    two($b);
    }

    function 
    two($b) {
      echo 
    $b;


  3. #3
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    263
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you; I am currently doing your proposal to solve such an issues, but I'm trying to find a way without passing arguments...

  4. #4
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    If I may.. why?

  5. #5
    SitePoint Addict
    Join Date
    Oct 2008
    Posts
    263
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    At some point it might be needed, and also simply to learn whether that possibility exists in PHP or not...

  6. #6
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    global calls for variables in the global scope.

    $b is not in the global scope. Try globalizing $b inside your one function.

  7. #7
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,095
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    If at any point you ~need~ globals you're doing something horribly wrong. Seriously.
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  8. #8
    Keeper of the SFL StarLion's Avatar
    Join Date
    Feb 2006
    Location
    Atlanta, GA, USA
    Posts
    3,748
    Mentioned
    73 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by scallioxtx View Post
    if at any point you ~need~ globals you're doing something horribly wrong. Seriously.
    qft.


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
  •