Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: Variable Scope

  1. #1
    New Coder
    Join Date
    Jul 2007
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Variable Scope

    I have a variable that I declared outside a function. How can I access the variable from inside the function?

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    The correct method is to pass everything a function (or a class) needs as parameters in the function call. This allows functions (or a class) to be general purpose and reusable without any need to setup specifically named variables which could conflict with something the program is already using that variable for or another function (or class) that could be using a variable with the same name.

    When you write programs with several hundred or several thousand lines of code, you cannot be juggling variable names just because they have been used inside of functions or rewrite known working functions (or classes) to change a variable name just because any name has already been used.

    Consider the case of all the php functions. To the best of my recollection, NONE of them require you to setup a specifically named variable in your code before you call the function that then uses that variable inside of the function.

    In case you still want to do what you asked, I will leave it up to you to look up and read in the php manual how to do this (it's in there for anyone to find once they look.)
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Senior Coder
    Join Date
    Apr 2005
    Location
    Colorado, United States
    Posts
    1,208
    Thanks
    0
    Thanked 0 Times in 0 Posts
    CFMaBiSmAd is correct. Accessing an external variable isn't the best way to go about things. Superglobals such as $_POST are accessible. Globals can be used but just pass things you need through the function call.
    "$question = ( to() ) ? be() : ~be();"

  • #4
    New Coder
    Join Date
    Jul 2007
    Posts
    67
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thank you!

    Can you tell me why this code isn't working. When I run it, nothing is displayed.
    PHP Code:
        function sequence ($x,$counter)
        {
            
            if (
    $x == 1)
            {
                return 
    $counter;
            }
            else
            {
                if (
    $x == 0)
                {
                    
    $counter++;
                    
    sequence($x/2,$counter);
                }
                else
                {
                    
    $counter++;
                    
    sequence(3*$x 1,$counter);
                }
            }
        }
    echo 
    sequence(13,0); 
    I'm trying to solve this problem by the way:
    The following iterative sequence is defined for the set of positive integers:

    n n/2 (n is even)
    n 3n + 1 (n is odd)

    Using the rule above and starting with 13, we generate the following sequence:
    13 40 20 10 5 16 8 4 2 1

    It can be seen that this sequence (starting at 13 and finishing at 1) contains 10 terms. Although it has not been proved yet (Collatz Problem), it is thought that all starting numbers finish at 1.

    Which starting number, under one million, produces the longest chain?

    NOTE: Once the chain starts the terms are allowed to go above one million.

  • #5
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    you need a 'return' in front of the recursive calls:
    PHP Code:
    if ($x == 0

      
    $counter++; 
      return 
    sequence($x/2,$counter); 

    (and the same on the other one)
    My thoughts on some things: http://codemeetsmusic.com
    And my scrapbook of cool things: http://gjones.tumblr.com


  •  

    Posting Permissions

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