PDA

View Full Version : multiple returns in functions


MrNase
Sat 13th Dec '03, 7:17pm
Hey all :)

I have something like

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 ??

merk
Sat 13th Dec '03, 8:36pm
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.


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_laenge, etc..);
}


or


function ($gegner)
{
global $gegner, $gegner_laenge, etc;
$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.

Sparkz
Sat 13th Dec '03, 9:11pm
I'd go for something like this:


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.

MrNase
Sat 13th Dec '03, 10:25pm
Hey Sparkz :)

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

Thank you :)

Sparkz
Sun 14th Dec '03, 6:49am
Hey Sparkz :)

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

Thank you :)


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 =)

MrNase
Sun 14th Dec '03, 7:11am
thank you, that was what i was looking for :)