PHP Palindrome Number Program - javatpoint
Palindrome Number
A palindrome number is a number which remains same when its digits are reversed.
For example, number 24142 is a palindrome number. On reversing it we?ll get the same number.
Logic:
- Take a number.
- Reverse the input number.
- Compare the two numbers.
- If equal, it means number is palindrome
Palindrome Number in PHP
Example:
8. Modules and PHP Abstract class | Build a CMS using OOP PHP CMS tutorial MVC [2020]
- <?php
- function palindrome($n){
- $number = $n;
- $sum = 0;
- while(floor($number)) {
- $rem = $number % 10;
- $sum = $sum * 10 + $rem;
- $number = $number/10;
- }
- return $sum;
- }
- $input = 1235321;
- $num = palindrome($input);
- if($input==$num){
- echo "$input is a Palindrome number";
- } else {
- echo "$input is not a Palindrome";
- }
- ?>
Output:
Palindrome Number using Form in PHP
Example:
We'll show the logic to check whether a number is palindrome or not.
- <form method="post">
- Enter a Number: <input type="text" name="num"/><br>
- <button type="submit">Check</button>
- </form>
- <?php
- if($_POST)
- {
- //get the value from form
- $num = $_POST['num'];
- //reversing the number
- $reverse = strrev($num);
- //checking if the number and reverse is equal
- if($num == $reverse){
- echo "The number $num is Palindrome";
- }else{
- echo "The number $num is not a Palindrome";
- }
- }
- ?>
Output:
On entering the number 23432, we get the following output.
On entering the number 12345, we get the following output.