Search code examples
phpfunctionreturnexplode

How to return multiple values from a function


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.


Solution

  • 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 :)