function is_title_case(string $s): bool {
$newWord = true;
// Iterate through each character
for ($i = 0; $i < strlen($s); $i++) {
$ch = $s[$i];
if (ctype_space($ch)) {
$newWord = true;
} else {
if ($newWord) {
if (!ctype_upper($ch)) {
return false;
}
$newWord = false;
} else {
if (!ctype_lower($ch)) {
return false;
}
}
}
}
return true;
}
// Test cases
$tests = [
"Hello World",
"Hello world",
"hello World",
"Php Web Language",
"This Is Fine",
"This is Not Fine"
];
foreach ($tests as $t) {
echo "\"$t\" -> " . (is_title_case($t) ? "true" : "false") . PHP_EOL;
}
/*
run:
"Hello World" -> true
"Hello world" -> false
"hello World" -> false
"Php Web Language" -> true
"This Is Fine" -> true
"This is Not Fine" -> false
*/