The reverse of a string PHP Program with form and database
In this tutorial, we will learn about the followings.
- The reverse of a string- PHP Program
- The reverse of a string PHP Program with form and database
The reverse of a string- PHP Program.
In this example, programmer stores the values in the variables. However, if you interested to see the program about taking values in the form, please see example 2.
<?php
function rev_str($str)
{
$n = strlen($str);
if($n == 1)
{
return $str;
}
else
{
$n--;
return rev_str(substr($str,1, $n)) . substr($str, 0, 1);
}
}
print_r(rev_str('T4Tutorials.com')."\n");
?>
The reverse of a string PHP Program with form and database.
In this example, we will take the inputs from the user and store them in variables.
<html>
<head><title>Reverse String</title></head>
<body>
<form method="post">
Enter String:
<input type="text" name="str"/>
<input type="submit" name="ok"/>
</form>
<?php
if(isset($_POST['ok'])){
$s=$_POST['str'];
$rev=strrev($s);
echo"$rev";
$conn = new mysqli("localhost", "root", "","reverse");
$sql = "INSERT INTO MYTable (String, ReverseString)
VALUES ('$s', '$rev')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
?>
</body>
</html>
Database of the reverse of a string.
