How to return multiple values from a function

17,094

Solution 1

Return an array.

function process_request(){
    ...
    return array( $CardHolderResponseDescription, $MerchantResponseText, $AuthCode, $MerchantError );
}

And pick it up via:

$_result = process_request();
$CardHolderResponseDescription = $_result[0];
$MerchantResponseText = $_result[1];
...

Tip: use shorter vars for better reading :)

Solution 2

The simplest thing you can do is putting the return values in an array which you can access later:

return array("CardHolderResponseDescription"=>$CardHolderResponseDescription, "MerchantResponseText" => $MerchantResponseText, "AuthCode" => $AuthCode );

And later:

list($RespDesc, $MerchResp, $AuthCode, $MerchError) = $my_return_value

Solution 3

In your function process_request:

return array($CardHolderResponseDescription, $MerchantResponseText, $AuthCode, $MerchantError);

When calling your function:

list($RespDesc, $MerchResp, $AuthCode, $MerchError) = process_request($billingID,$order_total,$merchRef);
Share:
17,094
dpDesignz
Author by

dpDesignz

I love web development and have over 10 years’ experience in front end development, back end development, and graphic design. I love working with clients and making their ideas happen. I am a team person who can work well with others, but I also do well when working on my own. I love solving problems as a Full-Stack Developer with my passion for front and back end development and graphic design. What's you're latest problem? Maybe I can help. One thing I really enjoy doing is looking at someone else’ code and trying to work out what goes with what and what makes it tick. I mainly dabble in PHP mixed with some HTML, CSS (I LOVE CSS Grid), and some Javascript. I have my own PHP Framework called Cornerstone which I develop most of my client websites on.

Updated on July 29, 2022

Comments

  • dpDesignz
    dpDesignz almost 2 years

    I'm building a script to work with PxPost from Payment Express and I've used their sample code as can be found at http://www.paymentexpress.com/Technical_Resources/Sample_code_-_PHP/PX_Post_-_cURL.aspx

    How it works: It's built into an automated script that queries orders from my database, processes them, and returns a value.

    My only problem is I want the function to return more than one value, so this is what I've done.

    Code to run through functions (Line 201):

    $once_complete = process_request($billingID, $order_total, $merchRef);
    

    Which send the payment to be processed, that then gets the returns and processes the XML using the sample code. At the end of the code I've removed all the $html info and just replaced it with the following (line 111):

    return $CardHolderResponseDescription.":".$MerchantResponseText.":".$AuthCode.":".$MerchantError;
    

    Which should as far as I understand, return that to what started it. I then want to split those values and return them as strings using the following (line 202):

    list($RespDesc, $MerchResp, $AuthCode, $MerchError) = explode(":", $once_complete);
    

    But for some reason that's not working.

    I've tried echo-ing the return and it works fine then, but then after that it seems to disappear. What may be going wrong?

    You can see the entire page's code at http://pastebin.com/LJjFutne. This code is a work in progress.

    • DCoder
      DCoder over 11 years
      Instead of packing the values like that, consider returning an array (or even better, create a class to hold these values and return an object of that class). Please stop writing new code with the ancient mysql_* functions. They are no longer maintained and community has begun the deprecation process. Instead you should learn about prepared statements and use either PDO or MySQLi. If you care to learn, here is a quite good PDO-related tutorial.
    • dpDesignz
      dpDesignz over 11 years
      @DCoder Thanks for the tip. I'm reading that now. Will start converting my script. :). Would returning an array work with it being processed in the loops like it is?
  • dpDesignz
    dpDesignz over 11 years
    Thanks but for some reason it's still not returning anything to me
  • dpDesignz
    dpDesignz over 11 years
    Thanks but for some reason it's still not returning anything to me. I plan on dropping them down once it's setup. :)
  • dpDesignz
    dpDesignz over 11 years
    Wow thanks! I should have read this code properly the first time. Worked a treat. :D
  • Jam
    Jam almost 6 years
    Elegant solution¡ Thanks.