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 6 of 6
  1. #1
    New Coder
    Join Date
    Apr 2006
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Can one return more than one variable using the return; structure in PHP?

    Hi all,

    I want to return two arrays at the end of a function. Is this possible? If so, is this the correct syntax:

    PHP Code:
    return $array1$array2
    Thanks a lot.
    Last edited by ibnpaul; 05-01-2006 at 10:58 PM.

  • #2
    New Coder
    Join Date
    Apr 2006
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nevermind, that would make problems in assigning the results. Sorry for posting w/o thinking first.

    I'm just going to assign the second array to an element of the first.

  • #3
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,174
    Thanks
    19
    Thanked 66 Times in 65 Posts
    you could return the whole thing as a string.
    eg.
    return implode(",", $array1)."|".imploode(",",$array1);

    then explode it at the other end.
    eg.

    $returnarrays = explode("|",$returnVal);
    $array1 = explode("," $returnarrays[0]);
    $array2 = explode(",", $returnarrays[1]);

  • #4
    New Coder
    Join Date
    Apr 2006
    Posts
    36
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Very good idea. Thanks a lot.

  • #5
    teh Moderatorinator
    Join Date
    Sep 2004
    Location
    USA
    Posts
    2,472
    Thanks
    4
    Thanked 40 Times in 40 Posts
    Another way:
    PHP Code:
    <?php

    function rets()
    {
        
    $array1 = array('some jazz here''some more...');
        
    $array2 = array('another bunch of jibb''foobar');
        return array(
    $array1$array2);
    }

    list(
    $arr1$arr2) = rets();

    echo 
    '<pre>';
    print_r($arr1);
    print_r($arr2);
    echo 
    '</pre>';

    ?>

  • #6
    Senior Coder
    Join Date
    Sep 2005
    Posts
    1,791
    Thanks
    5
    Thanked 36 Times in 35 Posts
    Brandoe's way is a lot nicer, someone else coming along to your and seeing a delimited string being returned might get awfully confused.


  •  

    Posting Permissions

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