Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Results 1 to 6 of 6
  1. #1
    Senior Member ericbusch's Avatar
    Join Date
    Aug 2003
    Location
    Daejeon, South Korea
    Posts
    241
    Member #
    2779
    Is it possible to use a variable that was created within a function in the main body of the script?

    if so, how?
    eric
    Blogs For Sale - Each blog is packed with 1000s of signatures and ads are highly integrated into the site. Great CTR! Completely customizable from Admin CP. On Sale!
    Complete Websites For Sale - Fantastic looking sites rich with content. Come complete with domain names. I only have a couple of these.

  2.  

  3. #2
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    If you use the word "global" in front of your variablename (declared or produced inside the function), it will be available outside the function afterwards.
    S. Rosland

  4. #3
    Senior Member ericbusch's Avatar
    Join Date
    Aug 2003
    Location
    Daejeon, South Korea
    Posts
    241
    Member #
    2779
    Thanks, i was finally able to figure that out. so then i tried to take the first variable and stick it into a second function, but with no luck. how do you get a variable outside of a function into a function? do you still have to use global, because i tried that and it did not work.

    Here is an example of the problem. i can access the first variable in the second function(v) but i cant access the second variable in the second function( the second variable is created outside of both functions)

    PHP Code:
    <?php
    print i();
    ?>
    <br><hr><br><br>
    <?php
    print v();
    ?>




    <?php


    //////////////////////////////////////////////////////////////////////////////////////////
    function i($first) {
        
        
    $first 0;
        
        global 
    $first;

        for (
    $i 1$i <= 4$i++) {
            
            
    $first++;
            
            
    ?>
            
        
                    
            <table border=1><tr><td>Number: <?php echo $i?></td></tr></table>
            
            <?php
        
        
    }// end for



    }// end function

    //////////////////////////////////////////////////////////////////////////////////////////

    global $second;

    echo 
    "first:   > " $first " < <br>\n";

    $second = ($first 2.5);

    echo 
    "second:   > " $second " < <br>\n";






    //////////////////////////////////////////////////////////////////////////////////////////
    function v() {
        
        global 
    $second$first;
        
        for (
    $v 1$v <= 4$v++) 
        
        {
        
            echo 
    "test second: " $second;
            
            echo 
    "This is " $v " first " $first "<br>\n";
        
        }
    // end for

    }// end function

    //////////////////////////////////////////////////////////////////////////////////////////

    ?>
    you can see this page at http://www.ericbusch.net/function1.php

    do you see any errors?

    thanks eric
    Blogs For Sale - Each blog is packed with 1000s of signatures and ads are highly integrated into the site. Great CTR! Completely customizable from Admin CP. On Sale!
    Complete Websites For Sale - Fantastic looking sites rich with content. Come complete with domain names. I only have a couple of these.

  5. #4
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    Any variable declared in a script, is globally available for that particular script with the exception of functions (which are sort of incapsulated).
    i.e. writing "global" in front of a variable outside of a function, makes no difference. On the other hand, using "global" inside a function makes that variable available outside the function as well.

    The only variables available both outside and inside functions, are the "Superglobals". The superglobals have their own scope definition, and have hence other availability rules.

    The superglobals are $_POST, $_GET, $_REQUEST, etc.

    For a variable definition to be globally available throughout the script, you can define it like this:
    PHP Code:
    <?php

    $_SERVER
    ['number']=34;

    function 
    test()
      {
        echo 
    "This is the variable inside \"test\" function: ".$_SERVER['number']."<br />";
      }

    test();
    echo
    "And the same variable outside the function: ".$_SERVER['number'];

    ?>
    S. Rosland

  6. #5
    Senior Member filburt1's Avatar
    Join Date
    Jul 2002
    Location
    Maryland, US
    Posts
    11,774
    Member #
    3
    Liked
    21 times
    PHP Code:
    function foo($i)
    {
        global 
    $myvar;
        
    $myvar $i;
    }

    function 
    bar($i)
    {
        
    $myvar $i;

    The first function will set $myvar to $i. The second will do nothing because $myvar doesn't exist in the proper scope.
    filburt1, Web Design Forums.net founder
    Site of the Month contest: submit your site or vote for the winner!

  7. #6
    Senior Member ericbusch's Avatar
    Join Date
    Aug 2003
    Location
    Daejeon, South Korea
    Posts
    241
    Member #
    2779
    Thanks!
    I needed to know about the superglobal. It should work perfect for my appplication.

    Thanks for everyone's help!

    Eric
    Blogs For Sale - Each blog is packed with 1000s of signatures and ads are highly integrated into the site. Great CTR! Completely customizable from Admin CP. On Sale!
    Complete Websites For Sale - Fantastic looking sites rich with content. Come complete with domain names. I only have a couple of these.


Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 03:42 AM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com