SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Extending the Scope of Variables Inside Functions

    Hello all. OK, let's say I have this code:

    PHP Code:
    <?php
            
    function do_stuff()
            {
                
    $var_a "variable a<br />";
                
    $var_b "variable b";
                
                echo 
    $var_a;  // Outputs "variable a"
            
    }
        
    ?>
    And this...
    PHP Code:
    <?php
            
    include "include.php";
            
    do_stuff();
            echo 
    $var_b;  // Doesn't output anything.
        
    ?>
    The output of this is "variable a". Obviously the scope of $var_a and $var_b is only from their declaration to the end of the function.

    How do I make them globally accessible in the file in which they are included?
    - Fotinakis

  2. #2
    SitePoint Evangelist klik's Avatar
    Join Date
    Jan 2005
    Location
    Scotland, UK
    Posts
    547
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can't you just return the values?

    PHP Code:
    <?php
            
    function do_stuff()
            {
                
    $var_a "variable a<br />";
                
    $var_b "variable b";
                
                echo 
    $var_a;  // Outputs "variable a"
                
    return $var_b;
            }
    ?>

    PHP Code:
    <?php
            
    include "include.php";
            
    $var_b do_stuff();
            echo 
    $var_b;  //Outputs returned value
        
    ?>

  3. #3
    SitePoint Enthusiast
    Join Date
    Sep 2004
    Posts
    67
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I could, but what if I don't want to and I want to extend their scope?
    - Fotinakis

  4. #4
    SitePoint Evangelist klik's Avatar
    Join Date
    Jan 2005
    Location
    Scotland, UK
    Posts
    547
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then you will have to set them as Global variables.

  5. #5
    SitePoint Guru mwolfe's Avatar
    Join Date
    Mar 2005
    Posts
    912
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Then you will have to set them as Global variables.
    This is correct, but..
    although this seems like a nice way to solve problems here and there, it usually just creates more in the long run, and can make your code twice as hard to follow.

  6. #6
    SitePoint Evangelist klik's Avatar
    Join Date
    Jan 2005
    Location
    Scotland, UK
    Posts
    547
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, it affects the cohesion of the project and errors can easily propogate through the system. Using local variables is more efficient and allows each module to work independantly.


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
  •