How would I condense this php method to have less code yet still be readable and functional? What would you change in this code?
public function charCount($string, $max, $min) {
$stringCount = strlen($string);
if(isset($max) || isset($min)) {
//Testing Max Chars
if($stringCount > $max) {
return 'String length: ' . $stringCount . "<br />
Max Chars: " . $max . "<br />
Error: To Long";
}
else if($stringCount < $min) {
return 'String length: ' . $stringCount . "<br />
Min Chars: " . $max . "<br />
Error: To Short";
}
else {
return true;
}
}
else {
return true;
}
}
First, I would choose between returning a boolean or a string, but not both, the reason is to have an homogen interface.
However
falsecould be used in case of error.I’ll go for something like this: