Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP function to return the length of the longest string argument

Tags:

php

Eg:

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

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

like image 673
Egglabs Avatar asked Dec 08 '22 04:12

Egglabs


1 Answers

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()));
}
like image 136
cletus Avatar answered May 13 '23 14:05

cletus