Function question...

Discussion in 'PHP' started by x0x, Oct 4, 2009.

  1. #1
    I made my first function today. But it's not working properly.

    Why can't I access the variables outside the function from the function after calling the function?

    Here's an example:

    
    function subsinfos(){
    
    $var = "Hello";
    
    return $var;
    }
    
    PHP:

    Now if I call up the function like this:

    subsinfos();
    PHP:
    How can I access the $var that was returned from the function?

    It doesn't work if I do this:

    subsinfos();
    echo $var;
    PHP:
    Why? What's the use of return then? What am I missing?
     
    x0x, Oct 4, 2009 IP
  2. toonboon

    toonboon Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #2
    return is the variable that the function returns, you could kinda call it like being a variable.

    Let me explain that further =)

    if you call your function
    
    subsinfos();
    
    Code (markup):
    That would result in the 'variable' subsinfos() having the value you return within the function.

    so if you'd use either
    
    echo subsinfos();
    
    Code (markup):
    or
    
    $newvar = subsinfos();
    //do something with the var, or don't
    echo $newvar;
    
    Code (markup):
    it would work like a charm =)

    Be sure to check php.net or read this tutorial:
    http://tizag.com/phpT/
    Code (markup):
    that tutorial is really helpful for beginners ^^!

    edit: sorry for the link not being clickable and you having to copy-paste it, the restrictions eh...
     
    toonboon, Oct 4, 2009 IP
  3. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Oh. Thanks, but what if there are more variables returned? How would I pull them?

    return $var2, $var2, var3;

    Either of your examples only pulls $var2...

    The only way I managed to pull them all was to put them all in an array and return that array, but there must be a easier way.
     
    x0x, Oct 4, 2009 IP
  4. lmao

    lmao Guest

    Messages:
    93
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    you can only return one value from any function but u can return more then one value in the fucntion if u use return value as array or use the call by reference parameters like

    //normal function returnning only one value

    function subsinfos(){

    $var = "Hello";

    return $var;
    }

    $value=subsinfos();

    // function with call by refrence

    function subsinfos_r(&$var1,&$var2,&$var3)
    {

    $var1 ="value 1";
    $var2 ="value 2";
    $var3 ="value 3";
    }

    $somevar1="";
    $somevar2="";
    $somevar3="";
    subsinfos_r($somevar1,$somevar2,$somevar3);

    // now if u will echo these values u can see there values are now changed.now they contains value which we assigned in function
    echo($somevar1);
    echo($somevar2);
    echo($somevar3);

    // by this way u can return more then 1 values without using arrays
     
    lmao, Oct 4, 2009 IP
  5. toonboon

    toonboon Peon

    Messages:
    20
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    unfortunately using an array or the ByRef parameters are the easiest ways... If you have a function that really needs to return multiple values think about how closely related the values are and splitting it up into multiple functions and arrays.

    For example the info of one day such as: temperature, rainfall, sun hours, amount of traffic jams, traffic jam lengt, etc...

    I'd split it in a traffic array and a wheather array and have two functions, one for traffic and one for the wheather which both return an array of values.

    Hope I helped and gave you some ideas =)
     
    toonboon, Oct 4, 2009 IP
  6. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #6
    Thanks guys, learned a ton!
     
    x0x, Oct 4, 2009 IP
  7. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #7
    You can also make a variable global and it can be accessed from outside the function if remember right.

    global $varname; //define a global variable
    PHP:
     
    wd_2k6, Oct 4, 2009 IP
  8. x0x

    x0x Well-Known Member

    Messages:
    510
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    110
    #8

    Isn't the global command used to make variables outside the function accessible within the function not vice versa?
     
    x0x, Oct 5, 2009 IP
  9. premiumscripts

    premiumscripts Peon

    Messages:
    1,062
    Likes Received:
    48
    Best Answers:
    0
    Trophy Points:
    0
    #9
    eh, you shouldn't really use globals that way.

    You can try this:

    list($a, $b, $c) = func();

    Where func is:

    function func() {
    return array(1, 2, 3);
    }

    Then you can simply use $a, $b, $c.

    Or just do:

    $returned = func();

    and then $returned[0] etc.
     
    premiumscripts, Oct 5, 2009 IP
  10. orionoreo

    orionoreo Peon

    Messages:
    145
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10

    list way is the most common for returning an array of data
     
    orionoreo, Oct 5, 2009 IP