String reverse without using library function in php | PHP reverse string without strrev | String manipulation programs in php

PHP reverse string without strrev, This is very simple to reverse a String in PHP by using inbuild function strrev. But sometimes in php interview, interviewer may ask how to Reverse a String in PHP without using strrev function or String reverse without using library function in php

There are few simple steps to answer them

  • As you know, that string is a sequence of character, so that why it an array by default.
  • we will declare a variable $i = 0 and run while until end of that string, find the length of string by incremented the value of $i
  • will run another while loop, and start from max length of string, until it’s greater than equal to 0.

Let’s see an example

Reverse a String in PHP without using strrev function

<?php
$str = "This is string reverse logic without using any inbuild functions";
$rev = "";
$i = 0;

while(@$str[$i++] != NULL);

$i--;

while($i >= 0)
{
    $rev = $rev.@$str[$i--];
}
echo "Original String : ".$str;
echo "<br><br>reverse String : ".$rev; 
?>
String reverse without using in build function

Note: i have added @ because of PHP is not tightly coupled language, so it will throw notice. Because of @ it will stop showing notice.

Also Read,

Reverse a String in PHP using strrev function

<?php
$str = "This is string reverse using strrev functions";
echo "Original String : ".$str;
echo "<br><br>reverse String : ".strrev($str); 
?>
Reverse a String in PHP using strrev function

https://youtu.be/utLa1DyZWZ8

Comments me below, if you need any help in this.

Pradip Mehta

I am a well-organized professional in Drupal Development and PHP web development with strong script handling knowledge or automation process with PHP. I have advanced computer skills and am a proficient multitasker.

This Post Has 2 Comments

  1. meubel winkel

    In yesterday Interview I was asked that how to reverse a string with out using any string function like strrev() or strlen(). I found this example on website but it gives error.is it possible to do this without strlen().

  2. Pradip Mehta

    Hi Meubel, It’s working perfectly. There might be throwing notice because. PHP is a loosely typed language, So that’s why i have added @ symbol in $str variable. Still you have any doubt then email me your screenshot and code at learnwebtechwithpradip@gmail.com , Thank you very much for visiting this site and giving me feedback.

Comments are closed.