The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> PHP Development
|
PHP-General - Echo page contents of HTML page. Worth it?
Discuss Echo page contents of HTML page. Worth it? in the PHP Development forum on Dev Shed. Echo page contents of HTML page. Worth it? PHP Development forum discussing coding practices, tips on PHP, and other PHP-related topics. PHP is an open source scripting language that has taken the web development industry by storm.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

November 11th, 2012, 02:23 PM
|
|
Contributing User
|
|
Join Date: Jan 2006
Location: .Canada {border:1px; Temperature: Freezing.}
Posts: 54
 
Time spent in forums: 21 h 5 m 26 sec
Reputation Power: 8
|
|
|
PHP-General - 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;
PHP Code:
<?PHP
echo ("
<HTML>
Blah~Blah~Blah
");
if
[Situation]
echo("
</HTML>
");
?>
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;
PHP Code:
<?PHP
Include stuff.
?>
<HTML>
Blah~Blah~Blah
<?PHP
if
[Situation]
echo();
?>
Blah~Blah~Blah
</HTML>
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, 02:57 PM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
Hi,
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.
|

November 11th, 2012, 07:06 PM
|
 |
Lost in code
|
|
|
|
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 Code:
<html>
<?php if(condition): ?>
<html>
<?php endif; ?>
<html>
|

November 12th, 2012, 12:15 AM
|
 |
pollyanna
|
|
Join Date: Jul 2012
Location: Germany
|
|
|
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, 08:39 AM
|
|
Contributing User
|
|
Join Date: Jun 2012
Posts: 47
Time spent in forums: 11 h 51 m 3 sec
Reputation Power: 1
|
|
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!
http://coding.smashingmagazine.com/...php-templating/
-Pete
__________________
Laterna Studio - Animation, Web Design, Video Production and Print Design
Psalm 73: 25-26
|

November 12th, 2012, 01:14 PM
|
 |
Contributing User
|
|
Join Date: Sep 2002
Location: Seattle, U.S.A.
Posts: 712
 
Time spent in forums: 4 Days 11 h 4 m 59 sec
Reputation Power: 11
|
|
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.
e.g.
PHP Code:
<?php
$myclass = new MyClass();
$navHtml = $myClass->generateNavigation();
$itemsClass = new Items();
$itemsHtml = $itemsClass->generateItemsTable();
?>
<html>
<head>><head>
<body>
<?php echo $navHtml ?>
<!-- a whole bunhc of html -->
<?php echo $itemsHtml ?>
<!-- a whole bunhc of html -->
</body>
</html>
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|