Announcement

Collapse
No announcement yet.

multiple returns in functions

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • multiple returns in functions

    Hey all

    I have something like
    Code:
     function ($gegner)
     {
     $gegner = htmlentities($gegner);
     $gegner_laenge = strlen($gegner);
     $gegner_punkte = strpos($gegner,e)+1;
     $gegner_punkte2 = strpos($gegner,d)+1;
     $gegner_punkte3 = strpos($gegner,a)+1;
     $gegner_punkte4 = strpos($gegner,r)+1;
     $gegner_bonus = strpos($gegner,z);
     return $gegner;
     }
    But how can i return $gegner_laenge, $gegner_punkte, ... too ??
    That's the end of that!

  • #2
    You cant do it the way you want to do it, but you could either, use an array of all that information, or, globalise them and set them that way.

    PHP Code:
    function ($gegner)
     {
     
    $gegner htmlentities($gegner);
     
    $gegner_laenge strlen($gegner);
     
    $gegner_punkte strpos($gegner,e)+1;
     
    $gegner_punkte2 strpos($gegner,d)+1;
     
    $gegner_punkte3 strpos($gegner,a)+1;
     
    $gegner_punkte4 strpos($gegner,r)+1;
     
    $gegner_bonus strpos($gegner,z);
     return array(
    $gegner$gegner_laengeetc..);
     } 
    or

    PHP Code:
    function ($gegner)
     {
    global 
    $gegner$gegner_laengeetc;
    $gegner htmlentities($gegner);
     
    $gegner_laenge strlen($gegner);
     
    $gegner_punkte strpos($gegner,e)+1;
     
    $gegner_punkte2 strpos($gegner,d)+1;
     
    $gegner_punkte3 strpos($gegner,a)+1;
     
    $gegner_punkte4 strpos($gegner,r)+1;
     
    $gegner_bonus strpos($gegner,z);
    // no more need for this if you do that return $gegner;
     

    Im pretty sure the second method will work, but im rusty with that.

    Comment


    • #3
      I'd go for something like this:

      PHP Code:
      function my_func ($input_value) {
          
      $return['size'] = strlen ($input_value);
          
      $return['upper'] = strtoupper ($input_value);
          
      // and so on
          
      return $return;

      That way you return an associative array of values.

      Troll, troll, troll your post, gently down the feed.
      Merrily, merrily troll along, a life is what you need!

      Comment


      • #4
        Hey Sparkz

        ....and $return[1]will return $return[upper]?

        Thank you
        Last edited by MrNase; Sun 14th Dec '03, 12:14am.
        That's the end of that!

        Comment


        • #5
          Originally posted by MrNase
          Hey Sparkz

          ....and $return[1]will return $return[upper]?

          Thank you
          PHP Code:
          function my_func ($input_value) { 
              
          $return['size'] = strlen ($input_value); 
              
          $return['upper'] = strtoupper ($input_value); 
              
          // and so on 
              
          return $return
          }

          $the_input "This is my input";

          $my_array my_func ($the_input); 
          $my_array['size'] should now contain 16 (length of string), while $my_array['upper'] should now contain the text 'THIS IS MY INPUT'.

          That's the beauty of associative arrays - you can reference them by key names, instead of key numbers =)

          Troll, troll, troll your post, gently down the feed.
          Merrily, merrily troll along, a life is what you need!

          Comment


          • #6
            thank you, that was what i was looking for
            That's the end of that!

            Comment

            widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
            Working...
            X