string reverse in php without using function

We can get string reverse through strrev() function. But string reverse should be without this function.
Example 1:- “John” should be “nhoJ”


<?php
$string = "John";
$arr_str = str_split($string);
$stlen = strlen($string);
for ($j = $stlen - 1; $j >= 0; $j--) {
	echo $arr_str[$j];
}
?>
Output:- nhoJ

Try it Yourself

Example 2:- If interviewer ask string reverse of words like “John working in TCS” should be “nhoJ gnikrow ni SCT”


<?php
$string="John working in TCS";
$str_into_arr = explode(" ", $string);
	$str_into_arr_count = count($str_into_arr);
	for ($i = 0; $i <= $str_into_arr_count - 1; $i++) {
		$arr_str = str_split($str_into_arr[$i]);
		$stlen = strlen($str_into_arr[$i]);
		for ($j = $stlen - 1; $j >= 0; $j--) {
			echo $arr_str[$j];
		}
		echo ' ';
	}
?>
Output:- nhoJ gnikrow ni SCT

Try it Yourself