November 11th, 2012, 03:23 PM
Echo page contents of HTML page. Worth it?
I remember when I first started learning PHP from an old friend of mine, for some reason he was always really proud that he parsed all of his HTML with PHP. Ex;
Does this make sense? Anyways, I haven't touched PHP in a long while, and I've forgotten most of what I've learned. Off topic, sorry! I haven't really had any issues with programming like this in the past, but I'm just looking for some insight..
I've checked Google and most people refer to this with file_get_contents(). Anyways, I was just wondering if there's a difference between the above argument, and the following;
Assuming you'd run multiple PHP lines. Sorry if this is a bogus question, but I appreciate the response and the time you're taking in answering.
November 11th, 2012, 03:57 PM
both is bad, because it's a wild mixture of programming logic (PHP) and presentation (HTML). You can do that, and it's still pretty common among amateur programmers, but it leads to complicated spaghetti code and several problems. For example, if you just want to change some HTML, you have to dig deep into the application logic and fumble with the PHP stuff (potentially breaking it). That's obviously a bad solution.
Modern application strictly separate PHP and HTML by using a template engine like Smarty. All the HTML is put into separate files (templates) and then managed by the PHP scripts.
Comments on this post
November 11th, 2012, 08:06 PM
It's also possible to separate your application logic and presentation logic without using a separate template engine/language. You can use PHP for both, just put them in separate files. PHP was itself designed as a template language, and many PHP programmers feel that using a separate language built on top of PHP, like Smarty, serves no purpose. In fact, most standard PHP frameworks do not use a separate template engine/language, but all of them do separate application logic and presentation.
When creating presentation templates with PHP it is common to use the alternative syntax for control structures. For example:
<?php if(condition): ?>
<?php endif; ?>
November 12th, 2012, 01:15 AM
Well, that's another topic you could discuss endlessly ...
The advantage of a separate template engine is that it has all kinds of special template features (like inheritance) and only those. When you use PHP for templates, you're basically limited to if/else and loops. And the separation of programming logic and templates is purely conventional. Nothing prevents you from putting logic into the views and vice versa (and many people tend to do that if they can).
But using PHP itself is more lightweight, of course. And you don't have to learn another language.
Anyway, the point I was trying to get at is that you should in fact separate those two things. And I think we all agree on that one ...
November 12th, 2012, 09:39 AM
This is a great, brief tutorial that explains the model-view-controller (MVC) pattern that is popular. It's basically a practical application of the "separation" that E-Oreo and Jacques1 were discussing. Good luck!
- Animation, Web Design, Video Production and Print Design
Psalm 73: 25-26
November 12th, 2012, 02:14 PM
I agree with Jaques and E-Oreo, that separating out your html from php is best practices. Definitely read the link about MVC.
Lots of amazing frameworks out there implement MVC: Codeigniter, Zend, Cake, etc. It's good way to get your feet wet in mvc implementations.
You don't always control projects your self, and sometimes you inherit messes. One medium step I often use in these cases would be to keep all of your PHP at the top of the page, with just a few echo in the right places in the html. This will keep it a bit clearer. And then in your own code you can implement some templates and not have to figure out how to rebuild all the existing pages.
$myclass = new MyClass();
$navHtml = $myClass->generateNavigation();
$itemsClass = new Items();
$itemsHtml = $itemsClass->generateItemsTable();
<?php echo $navHtml ?>
<!-- a whole bunhc of html -->
<?php echo $itemsHtml ?>
<!-- a whole bunhc of html -->