my php application looks this:
<?php // file: index.php // code... include( 'template/header.php' ); // more code, example: echo "subscribe our newsletter here: ...";
and template file:
<?php // file: template/header.php $user = get_loggedin_user() if ( ! $user ) { echo 'please log in'; return; // question statement! } // long php code follows, simplicity reduce to: echo "you logged in $user";
you see in condition in template/header.php use return
skip header if user not logged in. not want use else
because code follows quite long , nested, want avoid adding nesting level here...
this usage of return
seems work correctly on php 5.6
question:
- is correct, or there more appropriate way skip rest of header file?
- you know of notices/warnings/errors php throw here?
what thow custom exception, because can catch exceptions, when call function login validation in it.
return should sufficient if php code in current scope, return not sufficient when validating if user logged in within function.
to demonstrate have attached 2 php files below, index.php file assume file you're executing , functions file called func.php.
index.php
<?php require('func.php'); try { $user = get_loggedin_user(); } catch(notloggedinexception $e) { echo 'do redirect here'; die(); } var_dump($user); // shows user value
func.php
<?php class notloggedinexception extends exception { } function get_loggedin_user() { throw new notloggedinexception("user not logged in"); }
Comments
Post a Comment