如何从一个函数返回多个值?

时间:2015-07-08 13:14:05

标签: php

我在php中创建简单的web,我想从一个函数返回多个变量。这可能与否。可能比怎么样?

4 个答案:

答案 0 :(得分:2)

将它们放入数组中:

// [A] a WebRequest(): which syntax-mode used + <genetators> / <content-state> of all data-payloads used to execute a WebRequest()-call
// ------------------

   aServerRESPONSE = WebRequest( "GET",
                                 "http://stackoverflow.com/users/3666197/user3666197",
                                 NULL,
                                 NULL,
                                 60000,
                                 aPostPAYLOAD,
                                 0,
                                 aRetResult_STORE,
                                 aRetHeadersSTORE
                                 );

// [A.1] method    [in]-> a HTTP method used in a WebRequest() call.
// [A.2] url       [in]-> a URL  target used in .
// [A.3] headers   [in]-> a string of Request headers in a format of "key: value" pairs, separated by a line break "\r\n".
// [A.4] cookie    [in]-> a Cookie setup value.
// [A.5] referer   [in]-> a Referer header value setup for the HTTP request.
// [A.6] timeout   [in]-> a WebRequest() timeout for sync-waiting in [ms].
// [A.7] data[]    [in]-> a WebRequest() HTTP message body.
// [A.8] data_size [in]-> a size of the data[] array under [A.7].
// [A.9] result[]        <-[out] storage for server response data.
// [A.A] result_headers  <-[out] storage server response headers.


// [B] any value returned from server, once a WebRequest() under [A] was executed
// --------------------

// [B.1] an MQL4-engine <Error-STATE>

   print GetLastError();

// [B.2] a WebRequest()-call's returned value

   print aServerRESPONSE;

// [B.3] as setup under [A.9] aRetResult_STORE

// [B.4] as setup under [A.A] aHeadersSTORE

所以在此之后,你可以说:

function myFunction() {
    return array('name' => 'Joe', 'birthday' => '1976. 08. 29');
}

如果您的阵列没有修复,那么您可以遍历键和项目:

$data = myFunction();
echo $data['name']."\n";
echo $data['birthday'];

答案 1 :(得分:1)

您可以返回一个数组

function doSth(){
    $a = 2;
    $b = 5
    $c = 9;
   return array($a,$b,$c);
}

然后使用list()方法获取单个值:

list($a, $b, $c) = doSth();
echo $a; echo $b; echo $c;

答案 2 :(得分:0)

您可以使用数组和list()函数执行此操作:

function doSomething(){
  // do stuff
  return [$a, $b];
}

list($a, $b) = doSomething();

答案 3 :(得分:-1)

< ?php

function multiple_returns()
{
    return array( 1, 2, "three" );
}

?>