Write a Program to Check Palindrome Number in PHP.
Method 1
function check_palindrome($number_input){ $check_number = $number_input; $sum = 0; while(floor($number_input)){ $reminder = $number_input%10; $sum = $sum *10+$reminder; $number_input = $number_input/10; } return ($check_number == $sum)? TRUE : FALSE; } $input_number = 1221; if(check_palindrome($input_number)){ echo "Yes Palindrome Number"; }else{ echo "No Palindrome Number"; } // Yes Palindrome Number
Method 2
function check_palindrome($number){ $string = (String)$number; // Convert to String $string_array = str_split($string); //Split into Array $reverse_array = array_reverse($string_array); // Reverse Array $reverse_string = implode('',$reverse_array); // Recombine Array to String return ($string == $reverse_string)? TRUE : FALSE; //Return True & False } $input_number = 121; if(check_palindrome($input_number)){ echo "Yes Palindrome Number"; }else{ echo "No Palindrome Number"; } //Yes Palindrome Number
Method 3
function check_palindrome($number){ $string = (String)$number; // Convert to String $reverse_string = strrev($string); // Reverse the String return ($string == $reverse_string)? TRUE : FALSE; //Return True & False } $input_number = 121; if(check_palindrome($input_number)){ echo "Yes Palindrome Number"; }else{ echo "No Palindrome Number"; } // Yes Palindrome Number
Method 4
function check_palindrome($number){ $string = (String)$number; $reverse_string=""; $string_length = strlen($string); // Used to find String word length. for($i=($string_length-1);$i>=0;$i--){ $reverse_string .= $string[$i]; } return ($string == $reverse_string)? TRUE : FALSE; //Return True & False } $input_number = 1221; if(check_palindrome($input_number)){ echo "Yes Palindrome Number"; }else{ echo "No Palindrome Number"; } // Yes Palindrome Number
Live Check Palindrome Number
Demo Page
(Visited 135 times, 1 visits today)
Written by: