forked from TheAlgorithms/PHP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCheckPalindrome2.php
More file actions
35 lines (30 loc) · 814 Bytes
/
CheckPalindrome2.php
File metadata and controls
35 lines (30 loc) · 814 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
31
32
33
34
35
<?php
/**
* This is a simple way to check palindrome string
* using php strrev() function
* make it simple
*
* @param string $string
* @param bool $caseInsensitive
* @return string
* @throws \Exception
*/
function checkPalindromeString(string $string, bool $caseInsensitive = true): string
{
//removing spaces
$string = trim($string);
if (empty($string)) {
throw new \Exception('You are given empty string. Please give a non-empty string value');
}
/**
* for case-insensitive
* lowercase string conversion
*/
if ($caseInsensitive) {
$string = strtolower($string);
}
if ($string !== strrev($string)) {
return $string . " - not a palindrome string." . PHP_EOL;
}
return $string . " - a palindrome string." . PHP_EOL;
}