Reverse string in PHP without using string function PHP

Reverse words in a given string in PHP

In this tutorial, we will learn how to reverse strings and eventually words in a given string in PHP.

It has many built-in methods which can be used for building more dynamic and interactive web pages.

We can reverse a word in a string in PHP in many ways. We will see some of them here.

Basic Usage

Before we see how to reverse words in a string, we will see how to reverse a string in PHP.

Method 1 – Using a for loop

Given below is an illustration for reversing a string using a simple for loop.

= 0; $i--) < echo $inpStr[$i]; >echo $revStr; > #driver $inpStr = "CodeSpeedy Technologies"; reverseString($inpStr); ?>
The reversed string is: seigolonhceT ydeepSedoC

Explanation
We use the for loop to index through each character in the string and print it.

Читайте также:  Set disable button javascript

Method 2 – Using the built-in method strrev()

Given below is the illustration for reversing a string using strrev() method.

 #driver $inpStr = "CodeSpeedy Technologies"; echo "The reversed string is: " . reverseString($inpStr); ?>
The reversed string is: seigolonhceT ydeepSedoC

Explanation
We use the built-in method ‘strrev()’ for reversing the string. We create a function named ‘reverseString()’ with the input string as its parameter and return the reverse of the string from it.

Now, we will see how to reverse words in a string. Given below is the illustration for the following to reverse the words in a string.

The reversed string words are: Technologies CodeSpeedy

Explanation
In the following program, we convert the string to an array by splitting it with spaces. We are reversing the array using the ‘array_reverse()’ method and again joining the array back to form a string to print the reversed words string.

Источник

Reverse String in PHP

reverse string in php

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

Web development, programming languages, Software testing & others

  • 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.

Reverse String in PHP - 1

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.

Reverse String in PHP - 2

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).

Reverse String in PHP - 3

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.

Reverse String in PHP - 4

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.
 else < $len1--; return Reverse(substr($str1,1, $len1)) . substr($str1, 0, 1); >> $str1 = "PavanKumarSake"; print_r(Reverse($str1)); ?>

recursion technique and the substr()

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
 return $str2; > $str2 = "PAVANKUMARSAKE"; print_r(Reverse($str2)); ?>

library functions of 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.

This is a guide to Reverse String in PHP. Here we discuss the logic of reversing the input string and how to reverse a string using various loops with respective examples. You can also go through our other related articles to learn more –

25+ Hours of HD Videos
5 Courses
6 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

92+ Hours of HD Videos
22 Courses
2 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

83+ Hours of HD Videos
16 Courses
1 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

PHP Course Bundle — 8 Courses in 1 | 3 Mock Tests
43+ Hours of HD Videos
8 Courses
3 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5

Источник

Reverse String in PHP | PHP Tutorial

Reverse string in PHP; Through this tutorial, you will learn, how to reverse a string in PHP without using string function and how to reserve a string using string function, how to reverse a string using a loop in PHP, PHP reverse words in a string.

Reverse String in PHP

  • Reverse string in PHP without using string function PHP
  • Reserve a string with using string function in PHP

Reverse string in PHP without using string function PHP

Here will create a PHP program to reverse a string in PHP without using string function. Let’s follow the below steps for creating a PHP program for reverse a string without using function:-

  • Declare a PHP variable and assign the string to a variable
  • Using the assigned variable, Calculate the length of string
  • Declare a variable in which you have stored a reverse string
  • iterate the string with for loop
  • Concatenate the string inside the for loop
  • Print the reversed string
= 0 ; $i--) < // stored a string $revStr .= $str[$i]; >// print $revStr variable, it will return reverse string print_r($revStr); ?>

Source Code – Reverse string without using string function In php

     

Reverse string in PHP without using string function PHP

= 0 ; $i--) < // stored a string $revStr .= $str[$i]; >// print $revStr variable, it will return reverse string print_r($revStr); ?>

Reserve a string with using string function in PHP

You can reverse a string In PHP using the inbuilt PHP function strrev().

  • Declare a PHP variable and assign the string to a variable
  • Put a declare variable into inside the strrev() and print it

Source Code – Reverse a string with using strrev() Function In PHP

     

Reverse string in PHP with using strrev() string function PHP

Conclusion

In this reverse string in PHP tutorial. You have learned how to reverse a string in PHP without using any function In PHP and also learn how to reverse a string using inbuilt PHP function strrev().

Источник

Оцените статью