Posted on by Kalkicode
Code Recursion

Reverse string using recursion in php

Php program for Reverse string using recursion. Here more information.

// Reverse string using recursion in Php
class ReverseString
	// This is reversing the string elements recursively
	public	function reverseText($text, $location)
		// Base condition to stop the recursion process
		if ($location >= 0)
			// Recursive function call
			return strval($text[$location]).
            $this->reverseText($text, $location - 1);
		// When no character remaining
		return "";
	// This is handling the request process of reverse string elements
	public	function reverse($text)
		// Display given string elements
		echo "Before Text : [".$text."]", "\n";
		return $this->reverseText($text, strlen($text) - 1);
	public static
	function main($args)
		$task = new ReverseString();
		$text = "ABCDE";
		$text = $task->reverse($text);
		// After reverse	
		echo "After Text  : [".$text."]\n", "\n";
		$text = $task->reverse("654A321");
		// After reverse	
		echo "After Text  : [".$text."]\n", "\n";


Before Text : [ABCDE]
After Text  : [EDCBA]

Before Text : [654A321]
After Text  : [123A456]


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment