Sometimes I feel baffled when I am asked some kind of algorithmic questions. I think I need to keep the mindset of algorithms. That is my take away today.
<?php // Reverse string in a recursive way /* This is a general question, I believe. */ function getRevString($str, $count = 0) { $length = strlen($str); if ( $count < ($length/2) ) { // swapping characters according the string index --$length; $temp = $str[$count]; $str[$count] = $str[$length-$count]; $str[$length-$count] = $temp; // don't forget "return" statement in order to get the result return getRevString($str, ++$count); } return $str; } // testing the function $str = "Hello World"; $test = getRevString($str); echo "Result::" . $test;