SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just two quick question on functions;

    I understand that return; signifies the end of a function, but is it possible to have two "returns" if they are seperated by an if and else function?
    eg

    if (Condition A)
    {
    return A;
    }
    else
    {
    return B;
    }

    Is this OK?

    Also is there any difference between global and $GLOBALS - are they for different situations?

    eg are these the same:

    global $Variable_A;
    global $Variable_B;

    &

    $GLOBALS["Variable_A", "Variable_B"];

  2. #2
    SitePoint Member
    Join Date
    Feb 2001
    Location
    Utah
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can return multiple variables from functions in PHP.. To do it you'd need to pass and reference the function x number of variables (Won't work with constants). For instance, the following code will call function blah() and will reference variables $one and $two, change them, end the function and will then print out the two variables.

    <?

    function blah(&$one, &$two)
    {
    $one = 1;
    $two = 1;
    }

    $one = 0;
    $two = 0;

    blah($one,$two);

    print "$one, $two"; // Will print 1,1
    ?>

    Hope that helps..

    Sean McNamara
    http://www.netnexus.com/

  3. #3
    SitePoint Wizard silver trophy Karl's Avatar
    Join Date
    Jul 1999
    Location
    Derbyshire, UK
    Posts
    4,411
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In answer to your question, yes you can have more than one return statement separated by if/else blocks or any other conditional.
    Karl Austin :: Profile :: KDA Web Services Ltd.
    Business Web Hosting :: Managed Dedicated Hosting
    Call 0800 542 9764 today and ask how we can help your business grow.

  4. #4
    SitePoint Evangelist
    Join Date
    May 2000
    Location
    Canada
    Posts
    533
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    just a note: ... to make some tricky functions, you should know that as soon as a return is hit, the function ends..
    cogito, ergo sum

  5. #5
    AdSpeed.com Son Nguyen's Avatar
    Join Date
    Aug 2000
    Location
    Silicon Valley
    Posts
    2,241
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    global $var
    $GLOBALS[$var]

    Both do the same thing, get the value from outter scope (eg: main program)
    - Son Nguyen
    AdSpeed.com - Ad Serving and Ad Management Made Easy

  6. #6
    SitePoint Evangelist
    Join Date
    Oct 2000
    Posts
    430
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the help, its clarified a few things, but also brought up another related Qu.

    Is "returning" a variable necessary when using global variables - which I'm using a lot.

    For example can I get a function to alter two or more global varaibles outside the function, without returning either.

    For example - is this valid:
    <?
    function calculator()
    {
    global $A
    global $B
    $A = $A . $B ;
    $B = $B . $A ;
    return;
    }

    //Define variables first

    $A = 1;
    $B = 2;

    //do function
    function calculator();
    echo $A . "<br>";
    echo $B . "<br>";

    //do function again
    function calculator();
    echo $A . "<br>";
    echo $B; . "<br>";

    ?>

    If my thinking is correct this would echo

    12 <br>
    21 <br>
    1221 <br>
    2112 <br>

    Am I working along the right lines?

  7. #7
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ChilliBoy, technically what you suggest is correct. However, this is bad practice in programming. (In fact one of the philosophies of object oriented programming is to *prevent* what you propose with what is called "data hiding" - whereby in general, a variable should only have scope within its own class.)

    The reason this is bad practice is that it has the potential to lead to anarchy. Lets say there is a team of programmers working on a project and we are both on the team. You are writing function foo() and I am writing function bar(). In your function foo() you go changing the values of certain global variables. But, as the programmer of function bar(), I'm not aware of this. So in my function bar() I call your function foo() and then wonder why my code no longer works as expected because calling foo() also changed the values of global variables.

    It is our duty as programmers to conduct ourselves with the civility and high breeding one would exhibit when invited to tea with the Queen of England. We do not take a sandwitch from the tray until we are offered one. Similary, we do not go changing global variables outside the scope of our function unless we are offered to do so by receiving them as parameters in the call to our function as SeanM suggests.

    function blah(&$one, &$two)
    {
    $one = 1;
    $two = 1;
    }

    Here the caller of the function is *required* to pass the address of the variables. The caller is aware that you will most likey change their values. Everything happens in an open and civil exchange. In this way, you can think of a function as a "contract" to perform a certain service. You require the caller to pass you certain variables and in turn you may return a value. Both parties know what to expect, provided no one goes changing global variables behind someone elses back

    Well I hope my silly explaination at least amuses!
    Last edited by freakysid; Feb 12, 2001 at 06:41.


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
  •