My PHP application looks like this:
<?php
// File: index.php
// some code...
include( 'template/header.php' );
// More code, for example:
echo "Subscribe to our newsletter here: ...";
And template file:
<?php
// File: template/header.php
$user = get_loggedin_user()
if ( ! $user ) {
echo 'Please log in';
return; // Question is about this statement!
}
// Long PHP code follows, for simplicity reduce it to:
echo "You are logged in as $user";
You see in the condition in template/header.php I use return
to skip the header if user is not logged in. I do not want to use else
because the code that follows is quite long and nested, so I want to avoid adding another nesting level here...
This usage of return
seems to work correctly on php 5.6
Question:
- Is this correct, or is there a more appropriate way to skip rest of the header file?
- You know of any notices/warnings/errors that PHP could throw here?