Arcalypse Posted July 23, 2014 Share Posted July 23, 2014 Hi guys, so I tried searching this, but I had some issues being able to find the proper wording and keywords to query. Everything I searched PHP, HTML, Coding Standard, Coding Style, Coding, Style, Standard, etc. I got varying results. With that being said, if anyone knows of a topic that already addresses this, please feel free to link it and I will gladly do the footwork. ^ TL;DR --> I searched, but couldn't find a topic for this... Alright, now down to the brass tacks... I am having some trouble building a function in PHP that will display the header. I believe (if I am not mistaken) that I have seen people warning against peppering PHP code with echo <HTML HERE> throughout your code. I can sort of understand why, but I admit I am not completely sure why this is. I am trying to figure out a way to display HTML without doing the following... <?php // -------------------------------------------------------------------------- // ADMIN BAR HANDLER // -------------------------------------------------------------------------- /** * First we need to determine if the user * is logged in or not. After determining * the user's logged in status, we will then * choose to display the admin bar or * redirect the user to the login page. */ function admin_bar() { if ( defined( USER_IS_LOGGED_IN ) ) { echo "<div class='admin_bar'> Admin Bar Here </div>"; } else if ( defined( USER_IS_NOT_LOGGED_IN ) ) { header( "Location: " . LOGIN_PAGE ) ; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/290076-php-html-practice/ Share on other sites More sharing options...
Jacques1 Posted July 23, 2014 Share Posted July 23, 2014 Not the mixture of PHP and HTML itself is bad but the mixture of application logic and graphical presentation. If you put HTML snippets into your code, then you make the code unreadable and the HTML inflexible. Even the tiniest design update requires you to wade through the entire spaghetti code. In the worst case, you even have to make major changes to the application logic just because you want the baked-in HTML to look differently. So, yes, do separate the two aspects. The minimum is to put all application logic on top on all HTML to the bottom. What on earth does an HTTP redirect have to do with some admin bar in the GUI? Why do you check the log-in status in a function which is supposed to do nothing but render an HTML snippet? This makes no sense. Do all your authentication, redirects and whatnot on top of the script and then render the HTML based on the results. Quote Link to comment https://forums.phpfreaks.com/topic/290076-php-html-practice/#findComment-1485993 Share on other sites More sharing options...
Arcalypse Posted July 23, 2014 Author Share Posted July 23, 2014 Jacques1 thanks for the reply man! Quote Link to comment https://forums.phpfreaks.com/topic/290076-php-html-practice/#findComment-1485996 Share on other sites More sharing options...
davidannis Posted July 23, 2014 Share Posted July 23, 2014 Perhaps I am missing something but you deal with two cases if USER_IS_LOGGED_IN is defined and if USER_IS_NOT _LOGGED_IN is defined. There is a third case, neither is defined - I would guess either that this is the first page request for a non-logged in user or that an error occurred. When I code for logins, I just check for LOGGED_IN and if not treat the user as not logged in. Quote Link to comment https://forums.phpfreaks.com/topic/290076-php-html-practice/#findComment-1485999 Share on other sites More sharing options...
Arcalypse Posted July 23, 2014 Author Share Posted July 23, 2014 Perhaps I am missing something but you deal with two cases if USER_IS_LOGGED_IN is defined and if USER_IS_NOT _LOGGED_IN is defined. There is a third case, neither is defined - I would guess either that this is the first page request for a non-logged in user or that an error occurred. When I code for logins, I just check for LOGGED_IN and if not treat the user as not logged in. This is not the actual code I have in place right now, although I generally follow the same rule of thumb. This should have just simply been an IF and ELSE code but either way. With that being said, I just get wary when I mix the HTML, I feel like I'm doing something VERY wrong. Quote Link to comment https://forums.phpfreaks.com/topic/290076-php-html-practice/#findComment-1486002 Share on other sites More sharing options...
Arcalypse Posted July 23, 2014 Author Share Posted July 23, 2014 You're right, I should have done just an IF and ELSE statement rather than an IF and IF ELSE. I appreciate all the feedback guys. Quote Link to comment https://forums.phpfreaks.com/topic/290076-php-html-practice/#findComment-1486006 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.