Reverse String in PHP | PHP Tutorial

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

Here will learn you two ways to reverse a string in PHP. The following two ways are:-

  • 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
<?php 
    //declare variable and assign a string 
	$str = "Hello";  

	//count length of string using php count function
	$count = strlen($str);  

	//Declare a variable in which you have stored a reverse string
	$revStr = '';
	for ($i=($count-1) ; $i >= 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

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Reverse string in PHP without using string function PHP </title>
</head>
<body>
<h4>Reverse string in PHP without using string function PHP </h4>

<?php 
    //declare variable and assign a string 
	$str = "Hello";  

	//count length of string using php count function
	$count = strlen($str);  

	//Declare a variable in which you have stored a reverse string
	$revStr = '';
	for ($i=($count-1) ; $i >= 0 ; $i--)   
	{  
	  // stored a string	
	  $revStr .= $str[$i]; 
	}

	// print $revStr variable, it will return reverse string  
	print_r($revStr);
?>  

</body>
</html>                                		

Reserve a string with using string function in PHP

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

Here you will learn how to reverse a string using the PHP strrev() function. We will create a PHP program to reverse a string using the inbuilt PHP function strrev(). Let’s follow the below steps for that.

  • Declare a PHP variable and assign the string to a variable
  • Put a declare variable into inside the strrev() and print it
<?php  
 $str = "Hello World!";  
 echo "Reverse string of $string is " .strrev( $str );  
?>

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

<!DOCTYPE html>
<html lang="en">
<head>
    <title>Reverse string in PHP with using strrev() string function PHP </title>
</head>
<body>
<h4>Reverse string in PHP with using strrev() string function PHP </h4>

<?php  
 $str = "Hello World!";  
 echo "Reverse string of $string is " .strrev( $str );  
?> 

</body>
</html>                                		

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

Recommended PHP Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *