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

Leave a Reply

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