mirror of
https://github.com/TheAlgorithms/PHP.git
synced 2025-01-17 15:18:13 +01:00
25 lines
606 B
PHP
25 lines
606 B
PHP
<?php
|
|
|
|
function is_palindrome(string $string, bool $caseInsensitive = true)
|
|
{
|
|
$string = trim($string); // Removing leading and trailing spaces
|
|
|
|
if (empty($string)) {
|
|
return false; // Returning false for an Empty String
|
|
}
|
|
|
|
if ($caseInsensitive) {
|
|
$string = strtolower($string); // Converting string to lowercase for case-insensitive check
|
|
}
|
|
|
|
$characters = str_split($string);
|
|
|
|
for ($i = 0; $i < count($characters); $i++) {
|
|
if ($characters[$i] !== $characters[count($characters) - ($i + 1)]) {
|
|
return false;
|
|
}
|
|
}
|
|
|
|
return true;
|
|
}
|