-
-
Notifications
You must be signed in to change notification settings - Fork 588
Expand file tree
/
Copy pathCheckPalindrome.php
More file actions
30 lines (23 loc) · 727 Bytes
/
Copy pathCheckPalindrome.php
File metadata and controls
30 lines (23 loc) · 727 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<?php
declare(strict_types=1);
/**
* Checks if a string is a palindrome
*/
function isPalindrome(string $string, bool $caseInsensitive = true): bool
{
$string = trim($string); // Removing leading and trailing spaces
if ($string === '' || $string === '0') {
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);
$counter = count($characters);
for ($i = 0; $i < $counter; $i++) {
if ($characters[$i] !== $characters[count($characters) - ($i + 1)]) {
return false;
}
}
return true;
}