Home  >  Article  >  Backend Development  >  Reverse String in PHP

Reverse String in PHP

PHPz
PHPzOriginal
2024-08-29 13:12:35448browse

In this article, we will learn about Reverse String in PHP. A string is the collection of characters that are called from the backside. These collections of characters can be used to reverse either the PHP function strrev() or by using the simple PHP programming code. For example on reversing PAVANKUMAR will become RAMUKNAVAP

ADVERTISEMENT Popular Course in this category PHP DEVELOPER - Specialization | 8 Course Series | 3 Mock Tests

Start Your Free Software Development Course

Web development, programming languages, Software testing & others

Logic:

  • At first, assigning the string to the variable.
  • Now calculate the string length.
  • Now create a new variable in order to store the reversed string.
  • Then Proceed with the loop like for loop, while loop and do while loop etc..
  • Concatenating the string inside of the loop.
  • Then display/print the reversed string which is stored in the new variable which is created in the middle.

Reverse String in PHP Using Various Loops

Various Loops like FOR LOOP, WHILE LOOP, DO WHILE LOOP, RECURSIVE METHOD, etc.. in order to Reverse the String in the PHP Programming Language.

 1. Using strrev() function

  • The below example/syntax will reverse the original string using already predefined function strrev() in order to reverse the string. In the example, a $string1 variable is created in order to store the string I mean the original string then the echo statement is used in order to print the original string and the reversed string with the help of strtev() function and then print by concatenating both.
  • You can check the output of the program below to check whether the string is reversed or not.

Code:

<?php
$string1 = "PAVANKUMAR";
echo "Reversed string of the $string1 is " .strrev ( $string1 );
?>

Output:

Reverse String in PHP

2. Using For Loop

  • In the below example, the “$string1” variable is created by assigning the string1 variable’s value as “PAVANSAKE”. Then the $length variable is created in order to store the length of the $string1 variable using the strlen() function. Now the for loop is used with initialization, condition and incrementation values as “$length1-1”, “$i1>=0”, “$i1 – -”. Then the $string1 variable’s index values are calling from the backside using the initialization of $i1 equals to $length-1.
  • At first, FOR LOOP will start with the value of the length of the “original string – 1” value. Then the loop starts running by checking the condition “i1>=0” then the original string’s index is called backward likewise loop will print each index value from the back for each iteration until the condition is FALSE. At last, we will get the Reverse of the string using FOR loop.

Code:

<?php
$string1 = "PAVANSAKE";
$length1 = strlen($string1);
for ($i1=($length1-1) ; $i1 >= 0 ; $i1--)
{
echo $string1[$i1];
}
?>

Output:

Reverse String in PHP

3. Using While Loop

  • Here in the below example while loops used in order to print the string which is reversed using the original string “PAVANSAKEkumar”.
  • In the below syntax/php program, at first, a $string1 variable is assigned with string value “PAVANSAKEkumar” then we calculate the length of the $string1 variable’s value and stored in the $length1 variable. It will be an integer always. Now the $i1 variable is the length of the string variable’s value -1($length1-1).
  • Now we start with the WHILE loop here using the condition “$i1>=0” then after that we will print the string’s index value from the back because the $i1 value is the last of the index value of the original string. After this, decrementing will be done in order to reverse the original string($i1=$i1-1).

Code:

<?php
$string1 = "PAVANSAKEkumar";
$length1 = strlen($string1);
$i1=$length1-1;
while($i1>=0){
echo $string1[$i1];
$i1=$i1-1;
}
?>

Output:

Reverse String in PHP

4. Using do while Loop

  • The program of reversing the string in PHP using the DO while loop is listed below. In the below example just like the WHILE LOOP. Every Logic term is the same as the WHILE LOOP but does follow is a bit different it will print the output first rather than checking the condition at first.
  • So even though if you print an output even if the condition result is FALSE.

Code:

<?php
$string1 = "PAVANSAKEkumaran";
$length1 = strlen($string1);
$i1=$length1-1;
do{
echo $string1[$i1];
$i1=$i1-1;
}
while($i1>=0)
?>

Output:

Reverse String in PHP

5. Using the Recursion Technique and the substr()

  • Here in the below example reversing the string is done using the recursion technique and the substr() function. Substr() function will help in getting the original string’s substring. Here a new function called Reverse() also defined which is passed as an argument with the string.
  • At each and every recursive call, substr() method is used in order to extract the argument’s string of the first character and it is called as Reverse () function again just bypassing the argument’s remaining part and then the first character concatenated at the string’s end from the current call. Check the below to know more.
  • The reverse () function is created to reverse a string using some code in it with the recursion technique. $len1 is created to store the length of the string specified. Then if the length of the variable equals to 1 i.e. one letter then it will return the same.
  • If the string is not one letter then the else condition works by calling the letters from behind one by one using “length – -“ and also calling the function back in order to make a recursive loop using the function (calling the function in function using the library function of PHP). $str1 variable is storing the original string as a variable. Now printing the function result which is the reverse of the string.

Code:

<?php
function Reverse($str1){
$len1 = strlen($str1);
if($len1 == 1){
return $str1;
}
else{
$len1--;
return Reverse(substr($str1,1, $len1))
. substr($str1, 0, 1);
}
}
$str1 = "PavanKumarSake";
print_r(Reverse($str1));
?>

Output:

Reverse String in PHP

6. Reversing String without using any library functions of PHP

  • The below syntax/ program example is done by swapping the index’s characters using the for loop like first character’s index is replaced by the last character’s index then the second character is replaced by the second from the last and so on until we reach the middle index of the string.
  • The below example is done without using any of the library functions. Here in the below example, $i2 variable is assigned with the length of the original string-1 and $j2 value is stored with value as “0” then in the loop will continue by swapping the indexes by checking the condition “$j2

Code:


Output:

Reverse String in PHP

Conclusion

I hope you understood the concept of logic of reversing the input string and how to reverse a string using various techniques using an example for each one with an explanation.

The above is the detailed content of Reverse String in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Previous article:Factorial in PHPNext article:Factorial in PHP