4

Eg:

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

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

1
  • 1
    I fail at seeing the question in this post. Commented Feb 22, 2010 at 8:24

1 Answer 1

15

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()));
}
Sign up to request clarification or add additional context in comments.

4 Comments

Only detail (which may or may not be wanted) is that if you call getLongestString("aaaaa","aa",99999999) it'll return 8, not 5.
Fatal error: func_get_args(): Can't be used as a function parameter
Srinivas Tamada: use a more recent PHP version. 5.3 has been out for quite a while now and has many improvements.
@Srinivas Tamada: Workaround: Store func_get_args() in a temporary variable and use this for the array_map.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.