tags:

views:

192

answers:

1

Eg:

GetLogestString("bae","afaaa","aaa") should return 5

GetLogestString("baeedfefe","afaaaa","aaa","bb") should return 9

+11  A: 

Use array_map(), strlen(), max() and func_get_args():

function getLongestString() {
  $args = func_get_args();
  return max(array_map('strlen', $args));
}

Edit: In PHP 5.2 you have to store the result of func_get_args() in a temporary variable. In PHP 5.3 you can do this:

function getLongestString() {
  return max(array_map('strlen', func_get_args()));
}
cletus
+1 great and easy way to come up with that so quickly
Sarfraz
Only detail (which may or may not be wanted) is that if you call getLongestString("aaaaa","aa",99999999) it'll return 8, not 5.
Vinko Vrsalovic
Fatal error: func_get_args(): Can't be used as a function parameter
Srinivas Tamada
Srinivas Tamada: use a more recent PHP version. 5.3 has been out for quite a while now and has many improvements.
janmoesen
@janmoesen Thank You
Srinivas Tamada
very nice and clean solution
Pragati Sureka
@Srinivas Tamada: Workaround: Store `func_get_args()` in a temporary variable and use this for the `array_map`.
Boldewyn
Fixed it to work on PHP 5.2.
cletus