Function to reverse a given string without using string reversal functions in PHP

Strrev is one existing function to reverse a string in PHP as below:

  1. $strToRev = “Hello World!”;
  2. $strReved = strrev($strToRev);
  3. echo “String Reversed from “.$strToRev.” to “.$strReved.” using strrev function in php “;
OUTPUT: String Reversed from Hello World! to !dlroW olleH using strrev function in php

But the question is how to reverse string BUT without using Existing function without using libraries of PHP Language.

To do so, first, we will have to calculate existing characters in the string.
after that, we will generate a for loop in reverse order to store the string from forwarding variable to another backward variable as below in the function:

  1. $strToRev = “Hello World!”;
  2. $strLen = strlen($strToRev);
  3. for($i=$strLen; $i>=0; $i–){
  4. $strReved.= $strToRev[$i];
  5. }
  6. echo “String Reversed from “.$strToRev.” to “.$strReved.” using our own function in php”;
OUTPUT: String Reversed from Hello World! to !dlroW olleH using our own function in php

Grover Harleen

Grover Harleen

Harleen Grover is running a growing web and mobile development company. He always focuses to learn something new and grow and share his knowledge with others same time. you can send him your questions also related to programming and Technology and He will always try to provide the Best outputs to you.

Leave a Reply

Your email address will not be published. Required fields are marked *