首页  >  文章  >  后端开发  >  PHP 中的回文

PHP 中的回文

WBOY
WBOY原创
2024-08-29 13:13:29558浏览

在了解PHP中的回文之前我们先研究回文。回文是指字符串的原串和反串没有变化。这意味着即使将字符串反转后,数字的回文也与原始数字相同。这也适用于数字。

广告 该类别中的热门课程 PHP 开发人员 - 专业化 | 8 门课程系列 | 3次模拟测试

开始您的免费软件开发课程

网络开发、编程语言、软件测试及其他

例如:

输入:12321
背面:12321

输入:Civic
背面:思域

要知道一个字符串或数字是否是回文,我们将使用 PHP 中的内置函数。

回文逻辑

获取回文背后的逻辑如下:

  1. 获取输入的数字或字符串。
  2. 使用内置函数获取输入数字或字符串的反转。
  3. 比较数字或字符串 - 输入和反向数字或字符串。
  4. 如果发现输入和反转相等,则说明该数字或字符串是回文。

如何在 PHP 中检查回文?

要检查数字的回文,我们将使用名为 strrev() 的内置函数

关于 PHP 中的 strrev() 函数: 该函数接受字符串和数字作为输入字符串。它对输入字符串执行反转,但不更改给定的字符串。它始终返回给定字符串的反转形式。

示例#1

在下面的程序中,我们有一个输入字符串 MADAM;在此字符串上应用 strrev() 函数。应用该函数后,结果返回准确的字符串 MADAM;然后,检查一个条件以确定输入和反转的字符串是否相等。

代码:

<?php
// example to get the palindrome of a string using built in function
//input string is MADAM
$input = "MADAM";
echo '<br> Input String '. $input;
//reverse of input string - MADAM - using strrev
$reverse = strrev($input);
echo '<br> Ouput String '. $reverse;
//condition to check if the input and the reverse of the string is equal or not
if($input == $reverse) {
echo '<br> '.$input.'  is a palindrome';
}
else {
echo '<br>'.$input.' is not a palindrome';
}
?>

输出:

PHP 中的回文

示例#2

如上面的程序所示,输入字符串是回文。现在让我们对数字应用相同的 strrev 函数来检查输入数字是否是回文。

代码

<?php
//example to get the palindrome of a number using built in function
// input string is 1234321
$input = 1234321;
echo '<br>'.'Input string '. $input;
//reverse of input string using strrev
$reverse = strrev($input);
echo '<br>'.'Reverse string '.$reverse;
//condition to check if the input and the reverse of the string is equal or not
if($input == $reverse) {
echo '<br>'.$input.' is a palindrome';
}
else {
echo '<br>'.$input.' is not a palindrome';
}
?>

输出:

PHP 中的回文

示例#3

在下面的程序中,我们使用了在名为 Palindrome_Function 的不同函数中定义的 strrev() 内置函数。因此,当调用此函数来反转字符串时,它会使用 strrev() 函数对输入字符串执行反转。您可以通过以下方式完成相同的程序。

代码

<?php
//example to get the palindrome of a number using built in function
function Palindrome_Function($input) {
// applying strrev() function to input string
$reverse = strrev($input);
//condition to check if reverse and input strings are same or not
if($reverse == $input) {
return true;
}
else
{
return false;
}
}
$input = 1995991;
//calling the reverse function
$result = Palindrome_Function($input);
if($result == TRUE) {
echo $input.' is palindrome';
}
else
{
echo $input.' is not palindrome';
}
?>

输出:

PHP 中的回文

示例#4

在下面的程序中,我们将输入一个不是回文数的数字并查看结果。

代码:

<?php
//example to get the palindrome of a number using built in function
function Palindrome_Function($input) {
$reverse = strrev($input);
if($reverse == $input) {
return true;
}
else
{
return false;
}
}
$input = 13241;
$result = Palindrome_Function($input);
if($result == TRUE) {
echo $input.' is palindrome';
}
else
{
echo $input.' is not palindrome';
}
?>

输出:

PHP 中的回文

示例#5

以下是程序,其中我们有一个包含输入文本框的表单。输入数字并提交表单后,我们就会得到结果,它告诉我们输入的数字是否是回文。

代码

<html>
<head>
<title>Palindrome Program</title>
</head>
<body>
<form method="post" action="program1.php">
<input type="text" name="number" value="" />
<input type="submit" name="submit" value="Submit" />
</form>
<?php
if(isset($_POST['number'])) {
$input = $_POST['number'];
$reverse = strrev($input);
if($reverse == $input) {
echo $input . 'is a palindrome';
}
else{
echo $input. 'is not a palindrome';
}
}
?>
</body>
</html>

输出:

PHP 中的回文

在下面的程序中,我们通过以下步骤在不使用 strrev() 函数的情况下获得数字的反转。

我们将在这里使用 while 循环:

  1. 获取输入号码
  2. 将数字除以 10 得到余数
  3. 将余数添加到新变量中,再乘以 10
  4. 将数字除以 10。

代码:

<?php
//example to check if number is palindrome or not without using function only for numbers
//defining the palindrome function
function Palindrome_Function($input) {
$number = $input;
$sum = 0;
//using while loop to get the reverse of the input number
while(floor($number))
{
$remainder = $number % 10;
$sum = $sum * 10 + $remainder;
$number = $number / 10;
}
if($sum == $input) {
return true;
}
else {
return false;
}
}
//passing the input number
$input_number = 1546451;
//calling the Palindrome_Function
$result = Palindrome_Function($input_number);
//check if the input is equal to output of palindrome_function
if($result){
echo "<br>"." $input_number is a Palindrome";   //if equal show $input is palindrome number
} else {
echo "<br>"."$input_number is not a Palindrome";  //if not equal show $input is not a palindrome number
}
?>

输出:

PHP 中的回文

结论 – PHP 中的回文

本文解释了什么是回文,如何判断一个数字是否是回文,以及如何知道输入字符串是否是回文。我希望这篇文章对您有所帮助。

以上是PHP 中的回文的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn