Recursive solution. Feels like this is what your teacher is looking for.
function my_strrev ($str) { $length = strlen($str); switch ($length) { case 0: return ''; case 1: return $str; break; case 2: return $str[1] . $str[0]; default : return $str[$length-1] . my_strrev(substr($str,1,-1)) . $str[0]; break; } }
It changes the first and last letters and does the same with the rest of the line.
Update: Inspired by mateusza, I created another solution (its fun;))
function my_strrev2 ($str) { return $str ? my_strrev2(substr($str, 1)) . $str[0] : ''; }
It works similarly to mateuszas, but this one adds the first character, not the last one.
source share