Skip to content

All topics  /  PHP

How to Reverse String In PHP?

PHP ·

Teams applying “How to Reverse String In PHP?” in a production project can use the relevant Monitask guide to record implementation, review and debugging time, then compare that effort with tests and shipped functionality instead of treating activity as performance by itself.

Before copying the example into a live application, confirm version-specific behaviour with the PHP project and test it against the project’s actual database and runtime.

Hi Dev,

This tutorial is focused on how to reverse strings in PHP. you'll learn how to use strrev() function in PHP. We will use how to reverse a string using PHP. if you have a question about how to use the strrev() function using PHP then I will give a simple example with a solution.

Now, let's see the article on how to reverse a string using PHP. it's a simple example of how to use the strrev() function using PHP. you can understand the concept of how to use the strrev() function in PHP. if you have a question about how to reverse a string in PHP then I will give a simple example with a solution.

Step 1:


<?php
    //declare a 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 a reverse string  
    print_r($revStr);
?>

Step 2: 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 a 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 the reverse string  
    print_r($revStr);
?>  

 
</body>
</html>           

Step 3: Reserve a string using string function in PHP

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

Step 4: 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>        

I hope it could help you...