Multiple returns from a function

后端 未结 30 2741
盖世英雄少女心
盖世英雄少女心 2020-11-22 06:12

Is it possible to have a function with two returns like this:

function test($testvar)
{
  // Do something

  return $var1;
  return $var2;
}
<
30条回答
  •  悲哀的现实
    2020-11-22 06:34

    I had a similar problem - so I tried around and googled a bit (finding this thread). After 5 minutes of try and error I found out that you can simply use "AND" to return two (maybe more - not tested yet) in one line of return.

    My code:

      function get_id(){
        global $b_id, $f_id;
        // stuff happens
        return $b_id AND $f_id;
      }
      //later in the code:
      get_id();
      var_dump($b_id);
      var_dump($f_id); // tested output by var_dump
    

    it works. I got both the values I expected to get/should get. I hope I could help anybody reading this thread :)

提交回复
热议问题