How to reverse a string without using Builtin Function in PHP

reverse

You wonder how to reverse a String without using a Built in Function strrev() in PHP?.
This question asked many times in Interviews.

I got a solution here.

Just see the below lines of codes.


<?php
$str = "SPIDERMAN";
$len = strlen($str);
for($i=$len-1; $i>=0; $i--)
{
echo $str[$i];
}
?>

First calculate the string length using the “strlen()” function.

After finding the length just loop through the string and print one by one.

Do you know how to reverse a string not using any Loops or any Built in function like “strlen()”?.

Get the solution here CLICK HERE

 

5 thoughts on “How to reverse a string without using Builtin Function in PHP

  1. program to reverse a string in php without using string functions in this program you are using strlen , i dont want to use this , is there any way to find string length by using
    array concepts

    1. $string = ‘RAVINDRA’;
      $i = 0;
      while(isset($string[$i])){
      $i++;
      }
      $i–;
      while(isset($string[$i])){
      echo $string[$i];
      $i–;
      }

  2. String index can help us.
    $string = ‘RAVINDRA’;
    $i = 0;
    while(isset($string[$i])){
    $i++;
    }
    $i–;
    while(isset($string[$i])){
    echo $string[$i];
    $i–;
    }

  3. $string = ‘HIREN’;
    $i = 0;

    while(isset($string[$i])){
    $i++;
    }

    $i–; // must be —

    while(isset($string[$i])){
    echo $string[$i];
    $i–; // must be —
    }

Leave a Reply

Theme: Overlay by Kaira
Agurchand