Palindrome or not using PHP


$num = $_POST["num"];

// convert number to string
$num_str = strval($num);

// reverse the string
$reverse_str = strrev($num_str);

// check if the original number is equal to the reversed number
if ($num_str === $reverse_str) {
echo "$num is a palindrome!";
} else {
echo "$num is not a palindrome.";

<form method="post">
<label for="num">Enter a number:</label>
<input type="number" id="num" name="num">
<input type="submit" value="Check">


This code first checks if the form is submitted using the $_SERVER[“REQUEST_METHOD”] variable.

If the form is submitted, it gets the number entered in the input box using $_POST[“num”].

It converts the number to a string using the strval function,

reverses the string using the strrev function, and checks if the original string is equal to the reversed string. If they are equal, the number is a palindrome.

Leave a Reply

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