PHP Program to reverse the string without using library function

bookmark

<?php
//PHP code to reverse the string without 
//using library function

//function definition 
//it accepts a string and returns the revrse string
function reverse_string($text){
    $rev = ''; //variable to store reverse string
    $i = 0; //counting length
    
    //calculating the length of the string 
    while(isset($text[$i])){
        $i++;
    }
    
    //accessing the element from the reverse
    //and, assigning them to the $rev variable 
    for($j = $i - 1; $j >= 0; $j--){
        $rev .= $text[$j];
    }    
    
    //returninig the reversed string
    return $rev;
}

//main code i.e. function calling
$str = "Hello world!";
$r_str = reverse_string($str);
echo "string is: ". $str . "<br/>";
echo "reversed string is: ". $r_str . "<br/>";

$str = "Welcome @ IncludeHelp.Com";
$r_str = reverse_string($str);
echo "string is: ". $str . "<br/>";
echo "reversed string is: ". $r_str . "<br/>";

?>


Output

string is: Hello world!
reversed string is: !dlrow olleH
string is: Welcome @ IncludeHelp.Com
reversed string is: moC.pleHedulcnI @ emocleW