Make a function that returns more than one value?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • The87Boy

    Make a function that returns more than one value?

    Is there any ways to make a function like system(), which returns more
    than one value (true or false) and (the array)

  • Jiri Fogl

    #2
    Re: Make a function that returns more than one value?

    What about reference-passed variable as an argument?

    For example:

    <?php

    function MyFunc ($input, &$return_arr ay)
    {
    if (!empty($input) )
    {
    $return_array = Array($input, date('H:m:s'));
    return true;
    }
    else
    return false;
    }

    $a = Array();
    if (MyFunc('Hello' , $a))
    print_r($a);
    else
    echo 'Empty input!';


    ?>

    For details on arguments passed by reference:




    The87Boy napsal(a):
    Is there any ways to make a function like system(), which returns more
    than one value (true or false) and (the array)
    >

    Comment

    • Oli Filth

      #3
      Re: Make a function that returns more than one value?

      The87Boy said the following on 25/09/2006 16:44:
      Is there any ways to make a function like system(), which returns more
      than one value (true or false) and (the array)
      A function can have only one return value. However, it can alter
      variables passed in by reference, which is what happens with system().

      See http://uk.php.net/manual/en/language.references.php.

      --
      Oli

      Comment

      • J.O. Aho

        #4
        Re: Make a function that returns more than one value?

        Oli Filth wrote:
        The87Boy said the following on 25/09/2006 16:44:
        >Is there any ways to make a function like system(), which returns more
        >than one value (true or false) and (the array)
        >
        A function can have only one return value. However, it can alter
        variables passed in by reference, which is what happens with system().
        >
        See http://uk.php.net/manual/en/language.references.php.
        Another option is to use an array to return the values you want to be
        returned, of course this will not be in the same manner as system() does, but
        can sometimes be a smother way to return multiple values.


        //Aho

        Comment

        • Johnny

          #5
          Re: Make a function that returns more than one value?


          "The87Boy" <the87boy@gmail .comwrote in message
          news:1159199042 .142987.271750@ i3g2000cwc.goog legroups.com...
          Is there any ways to make a function like system(), which returns more
          than one value (true or false) and (the array)
          >
          It is something I've done many times in c++ but not so much in PHP...
          that one thing returned could be an object which has whatever you want in
          it.
          here is the relevant line from the docs

          " Values are returned by using the optional return statement. Any type may
          be returned, including lists and objects."

          here is a quick example:
          <?php
          class stuff { # a container for all your stuff
          var $the_bool;
          var $the_array;
          function stuff($b,$a) {
          $this->the_bool = $b;
          $this->the_array = $a;
          }
          function get_bool () {
          return $this->the_bool;
          }
          function get_array () {
          return $this->the_array;
          }
          }
          $a[] = "bread";
          $a[] = "butter";
          $a[] = "salt";
          var_dump($a);
          echo "<br/>var dump thing :<br />";
          $thing = new stuff(true,$a);
          var_dump($thing );
          echo "<br />the bool is ".$thing->get_bool();
          $a1 = $thing->get_array();
          echo "<br/>var dump a1 is<br />";
          var_dump($a1);
          echo "<br />the array 0 is ".$a1[0];
          echo "<br />the array 1 is ".$a1[1];
          echo "<br />the array 2 is ".$a1[2];

          $arr[] = "49";
          $arr[] = "sugar";
          $arr[] = "37.294";

          function demo($bl,$ar) {
          $c = new stuff($bl,$ar);
          return $c;
          }
          $d = demo(true,$arr) ;
          echo "<br/><br/>d->bool returned from demo is ".$d->get_bool();
          $da = $d->get_array();
          echo "<br/>d->a[0] returned from demo is ".$da[0];
          echo "<br/>d->a[1] returned from demo is ".$da[1];
          echo "<br/>d->a[2] returned from demo is ".$da[2];

          ?>


          Comment

          • The87Boy

            #6
            Re: Make a function that returns more than one value?

            It was just what I was searching for, but I didn't now, it existed

            Jiri Fogl wrote:
            What about reference-passed variable as an argument?
            >
            For example:
            >
            [Some code...]
            >
            For details on arguments passed by reference:
            http://php.net/manual/en/functions.arguments.php

            Comment

            • Rudi Menter

              #7
              Re: Make a function that returns more than one value?

              The87Boy:

              Why not just returning an array (of arrays (of arrays (...))) ...?
              It was just what I was searching for, but I didn't now, it existed
              >
              Jiri Fogl wrote:
              >What about reference-passed variable as an argument?
              >>
              >For example:
              >>
              >[Some code...]
              >>
              >For details on arguments passed by reference:
              >http://php.net/manual/en/functions.arguments.php

              Comment

              • Markus Ernst

                #8
                Re: Make a function that returns more than one value?

                Jiri Fogl schrieb:
                What about reference-passed variable as an argument?
                >
                For example:
                >
                <?php
                >
                function MyFunc ($input, &$return_arr ay)
                {
                if (!empty($input) )
                {
                $return_array = Array($input, date('H:m:s'));
                return true;
                }
                else
                return false;
                }
                >
                $a = Array();
                if (MyFunc('Hello' , $a))
                print_r($a);
                else
                echo 'Empty input!';
                >
                >
                ?>
                Just as an addition - this special case could also be handled without a
                reference, for example:

                <?php
                function MyFunc ($input)
                {
                if (!empty($input) )
                return array($input, date('H:m:s'));
                else
                return false;
                }

                if ($a = MyFunc('Hello') )
                print_r($a);
                else
                echo 'Empty input!';
                ?>

                Or, if you need to change an existing array:

                <?php
                function MyFunc ($input, $arr)
                {
                if (!empty($input) ) {
                $arr[] = $input;
                $arr[] = date('H:m:s');
                return $arr;
                }
                else
                return false;
                }

                $arr = array('some', 'data');
                if ($new_arr = MyFunc('Hello', $arr))
                $arr = $new_arr;
                else
                echo 'Empty input!';
                print_r($arr);
                ?>

                --
                Markus

                Comment

                Working...