function VerifyAllUpperOrAllLowerOrIsCapitalized(string $word): bool {
$upper = 0;
$lower = 0;
$length = strlen($word);
for ($i = 0; $i < $length; $i++) {
$ch = $word[$i];
if (ctype_lower($ch)) {
$lower++;
} elseif (ctype_upper($ch)) {
$upper++;
}
}
// Case 1: all lowercase
if ($upper === 0) return true;
// Case 2: all uppercase
if ($lower === 0) return true;
// Case 3: capitalized (only first letter uppercase)
if ($upper === 1 && ctype_upper($word[0])) return true;
// Otherwise, mixed casing
return false;
}
/**
* Runs a test case and prints the result
*/
function runTest(string $word): void {
echo "Testing word: \"$word\"\n";
if (VerifyAllUpperOrAllLowerOrIsCapitalized($word)) {
echo "OK\n\n";
} else {
echo "Error\n\n";
}
}
runTest("PROGRAMMING");
runTest("programming");
runTest("Programming");
runTest("ProGramMing");
/*
run:
Testing word: "PROGRAMMING"
OK
Testing word: "programming"
OK
Testing word: "Programming"
OK
Testing word: "ProGramMing"
Error
*/