your image

PHP Palindrome Number Program - javatpoint

javapoint
Related Topic
:- PHP Java information technology

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]

  1. <?php  
  2. function palindrome($n){  
  3. $number = $n;  
  4. $sum = 0;  
  5. while(floor($number)) {  
  6. $rem = $number % 10;  
  7. $sum = $sum * 10 + $rem;  
  8. $number = $number/10;  
  9. }  
  10. return $sum;  
  11. }  
  12. $input = 1235321;  
  13. $num = palindrome($input);  
  14. if($input==$num){  
  15. echo "$input is a Palindrome number";  
  16. } else {  
  17. echo "$input is not a Palindrome";  
  18. }  
  19. ?>  

Output:

Palindrome Number using Form in PHP

Example:

We'll show the logic to check whether a number is palindrome or not.

  1. <form method="post">        
  2. Enter a Number: <input type="text" name="num"/><br>  
  3. <button type="submit">Check</button>  
  4. </form>  
  5. <?php   
  6.     if($_POST)  
  7.     {  
  8.         //get the value from form  
  9.         $num = $_POST['num'];  
  10.         //reversing the number  
  11.         $reverse = strrev($num);  
  12.           
  13.         //checking if the number and reverse is equal  
  14.         if($num == $reverse){  
  15.             echo "The number $num is Palindrome";     
  16.         }else{  
  17.             echo "The number $num is not a Palindrome";   
  18.         }  
  19. }     
  20.       ?>  

Output:

On entering the number 23432, we get the following output.

On entering the number 12345, we get the following output.

Comments