Strrev is one existing function to reverse a string in PHP as below:
OUTPUT: String Reversed from Hello World! to !dlroW olleH using strrev function in php
- $strToRev = “Hello World!”;
- $strReved = strrev($strToRev);
- echo “String Reversed from “.$strToRev.” to “.$strReved.” 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:
OUTPUT: String Reversed from Hello World! to !dlroW olleH using our own function in php
- $strToRev = “Hello World!”;
- $strLen = strlen($strToRev);
- for($i=$strLen; $i>=0; $i–){
- $strReved.= $strToRev[$i];
- }
- echo “String Reversed from “.$strToRev.” to “.$strReved.” using our own function in php”;