Home  >  Article  >  Backend Development  >  How to use PHP function to reverse numbers and print all palindrome numbers within a given range

How to use PHP function to reverse numbers and print all palindrome numbers within a given range

青灯夜游
青灯夜游Original
2021-08-12 16:57:393546browse

In the previous article "How to output all three-digit palindrome numbers through a PHP program", we took you through the general method of judging a three-digit palindrome number and introduced how to output it. The number of palindromes within 100~999, the method of counting the number of palindromes, interested friends can learn about it~

This article continues to show you how to determine the number of palindromes in PHP, this time there is no limit Range, given any number, we will determine whether the number is a palindrome!

Let’s take a look at my implementation method:

<?php
header("Content-type:text/html;charset=utf-8");
function hws($num) {
	if($num==strrev($num)){
		echo $num."是一个回文数<br><br>";
	}else{
		echo $num."不是一个回文数<br><br>";
	}
}
hws(5);
hws(11);
hws(12);
hws(212);
hws(213);
hws(1515);
hws(15151);
?>

The output result is:

How to use PHP function to reverse numbers and print all palindrome numbers within a given range

In the above example, we use There is a PHP built-in function-strrev(), which can reverse a string; using this function, we can reverse a number (example 213) into the string "321".

Then use the "==" operator to compare whether the number 213 and the string "321" are equal; they are obviously not equal, so 213 is not a palindrome number.

The comparison performed by the equality (==) operator is loose. It only compares the values ​​​​of the variables on both sides, not the data types; if the two values ​​​​are the same, it returns a true value, if the two If the values ​​are not the same, a false value is returned.

Recommended reading: "A simple comparison of equation (==) and identity (===) operators in PHP"

Isn’t it, very simple , not as much calculation as above!

Let’s make it more difficult. Let’s print all the palindrome numbers in a given range and count the number of palindromes. First try all palindrome numbers within 100:

<?php
header("Content-type:text/html;charset=utf-8");
$num=0;
for($i=0;$i<100;$i++){
	if($i==strrev($i)){
		echo $i." ";
		$num++;
	}
}
echo "<br><br>100以内的回文数共有".$num."个";
?>

Output result:

How to use PHP function to reverse numbers and print all palindrome numbers within a given range

Then all palindromes between 100~1000 Number

<?php
header("Content-type:text/html;charset=utf-8");
$num=0;
for($i=100;$i<1000;$i++){
	if($i==strrev($i)){
		echo $i." ";
		$num++;
	}
}
echo "<br><br>100~1000间的回文数共有".$num."个";
?>

Output result:

How to use PHP function to reverse numbers and print all palindrome numbers within a given range

ok, encapsulated into a functional function:

<?php
header("Content-type:text/html;charset=utf-8");
function hws($a,$b) {
	$num=0;
	for($i=$a;$i<$b;$i++){
		if($i==strrev($i)){
			echo $i." ";
			$num++;
		}
	}
	echo "<br><br>$a ~ $b 间的回文数共有".$num."个<br><br><br>";
}
hws(0,100);
hws(100,1000);
?>

Output result:

How to use PHP function to reverse numbers and print all palindrome numbers within a given range

Okay, that’s all. If you want to know anything else, you can click this. → →php video tutorial

The above is the detailed content of How to use PHP function to reverse numbers and print all palindrome numbers within a given range. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn