Here’s a Palindrome Checker in PHP along with an explanation of how it works. A palindrome is a word, phrase, number, or other sequence of characters that reads the same forward and backward (ignoring spaces, punctuation, and capitalization).
Project Description
The palindrome checker will:
Allow users to input a string or number.
Check if the input is a palindrome.
Display the result indicating whether or not the input is a palindrome.
Complete Code
<?php
// Initialize variables
$result = "";
$error_message = "";
// Function to check if a string is a palindrome
function isPalindrome($input) {
// Remove spaces, special characters, and convert the input to lowercase
$processed_input = strtolower(preg_replace("/[^A-Za-z0-9]/", "", $input));
// Reverse the processed string
$reversed_input = strrev($processed_input);
// Check if the original processed string is equal to the reversed string
return $processed_input === $reversed_input;
}
// Check if the form has been submitted
if (isset($_POST['submit'])) {
$input = $_POST['input'];
// Validate that the input is not empty
if (!empty($input)) {
// Check if the input is a palindrome
if (isPalindrome($input)) {
$result = "Yes, '$input' is a palindrome!";
} else {
$result = "No, '$input' is not a palindrome.";
}
} else {
$error_message = "Please enter a string or number.";
}
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Palindrome Checker</title>
</head>
<body>
<h1>Palindrome Checker</h1>
<!-- Display error message if any -->
<?php if ($error_message): ?>
<p style="color: red;"><?php echo $error_message; ?></p>
<?php endif; ?>
<!-- Form for input -->
<form method="post" action="">
<label for="input">Enter a string or number:</label>
<input type="text" name="input" required value="<?php echo isset($input) ? htmlspecialchars($input) : ''; ?>">
<br><br>
<button type="submit" name="submit">Check Palindrome</button>
</form>
<!-- Display the result -->
<?php if ($result): ?>
<h2><?php echo $result; ?></h2>
<?php endif; ?>
</body>
</html>
Explanation of the Code
1. Function to Check for Palindrome
function isPalindrome($input) {
$processed_input = strtolower(preg_replace("/[^A-Za-z0-9]/", "", $input));
$reversed_input = strrev($processed_input);
return $processed_input === $reversed_input;
}
preg_replace(“/[^A-Za-z0-9]/”, “”, $input): This removes all non-alphanumeric characters (i.e., spaces, punctuation, special characters) from the input.
strtolower(): Converts the input string to lowercase to make the check case-insensitive.
strrev(): Reverses the processed string.
The function checks if the processed string is the same as its reversed version. If they are the same, the input is a palindrome.
2. Handling Form Submission and Input Validation
if (isset($_POST['submit'])) {
$input = $_POST['input'];
if (!empty($input)) {
if (isPalindrome($input)) {
$result = "Yes, '$input' is a palindrome!";
} else {
$result = "No, '$input' is not a palindrome.";
}
} else {
$error_message = "Please enter a string or number.";
}
}
This block checks if the form has been submitted using isset($_POST[‘submit’]).
It retrieves the user input from the form using $_POST[‘input’].
If the input is not empty, the script calls the isPalindrome() function to check if the input is a palindrome. The result is stored in the $result variable.
If the input is empty, an error message is displayed asking the user to provide valid input.
3. HTML Form for Input
<form method="post" action="">
<label for="input">Enter a string or number:</label>
<input type="text" name="input" required value="<?php echo isset($input) ? htmlspecialchars($input) : ''; ?>">
<br><br>
<button type="submit" name="submit">Check Palindrome</button>
</form>
The form allows users to input a string or number for palindrome checking.
The form uses the POST method to submit the data to the same PHP script.
4. Displaying the Result
<?php if ($result): ?>
<h2><?php echo $result; ?></h2>
<?php endif; ?>
If a result is generated (i.e., after the form is submitted), it is displayed on the webpage.
How the Palindrome Checker Works
User Input: The user enters a string or number in the input field.
Form Submission: When the user clicks the “Check Palindrome” button, the form data is sent to the PHP script via POST.
Validation: The script checks if the input is not empty. If the input is valid, it proceeds to the palindrome check.
Palindrome Check: The isPalindrome() function processes the input by removing non-alphanumeric characters and converting it to lowercase. The script then checks if the processed string is the same as its reverse.
Result Display: The result (“Yes, it is a palindrome!” or “No, it is not a palindrome.”) is displayed.
Example Inputs and Outputs
Input: “madam”
Output: Yes, ‘madam’ is a palindrome!
Input: “Hello”
Output: No, ‘Hello’ is not a palindrome.
Input: “A man, a plan, a canal, Panama”
Output: Yes, ‘A man, a plan, a canal, Panama’ is a palindrome!
Potential Improvements
Handling Numbers: Extend the functionality to handle numeric palindromes (e.g., 12321).
Multiple Palindrome Types: Add support for checking word-level palindromes in phrases or sentences.
Styling: Use CSS to style the form and result for a better user experience.
This simple PHP project is great for practicing form handling, string manipulation, and conditional logic. It shows how to work with both client-side input and server-side processing efficiently.
