Well lets see.
Short version: Here’s how i would have it.
function getPage()
:string
{
//I added a safety check here in case strrchr returns FALSE.
$PAGE = substr(strrchr($_SERVER['REQUEST_URI'], '/') ?: " ",1);
$PAGE = (''!==$PAGE && 'index.php' !==$PAGE) ? $PAGE : 'home.php';
if( !file_exists('sitepages/' . $PAGE)) :
$PAGE = 'missing-file';
endif;
return str_replace(".php","",$PAGE);
}
Now, I admit there is some loss of readability in my compression. But let me isolate one block that i think could be simplified in your original code without as much disruption:
Before the first line, you know that the first character of the string is a /, because you created the string by reverse searching for a slash. Furthermore, you know that there is exactly one slash.
The only way this is not the case is if strrchr returned FALSE, but that would have failed immediately anyway, because you wouldn’t be able to str_replace a boolean without causing typecasting. (See my extra check in my version as to how I force this to be a valid string regardless)
Simplification: Substring the initial result of strrchr, remove the entire if block; check if page is the empty string at the same time you check if it’s set to index, because the action in both cases is the same.
Any time you’ve got an empty clause in an if statement, you know you can simplify. Since this is an empty If, invert the test and make your Else clause your If clause.