PHP Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPHP Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 11th, 2012, 02:23 PM
aLT_w0rm aLT_w0rm is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2006
Location: .Canada {border:1px; Temperature: Freezing.}
Posts: 54 aLT_w0rm User rank is Lance Corporal (50 - 100 Reputation Level)aLT_w0rm User rank is Lance Corporal (50 - 100 Reputation Level)aLT_w0rm User rank is Lance Corporal (50 - 100 Reputation Level) 
Time spent in forums: 21 h 5 m 26 sec
Reputation Power: 8
Send a message via AIM to aLT_w0rm Send a message via MSN to aLT_w0rm Send a message via Yahoo to aLT_w0rm
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.

Reply With Quote
  #2  
Old November 11th, 2012, 02:57 PM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,838 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 6 h 6 m 58 sec
Reputation Power: 813
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.
Comments on this post
ptr2void agrees!

Reply With Quote
  #3  
Old November 11th, 2012, 07:06 PM
E-Oreo's Avatar
E-Oreo E-Oreo is offline
Lost in code
Click here for more information.
 
Join Date: Dec 2004
Posts: 7,931 E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)E-Oreo User rank is General 90th Grade (Above 100000 Reputation Level)  Folding Points: 945 Folding Title: Novice Folder
Time spent in forums: 2 Months 7 h 43 m 47 sec
Reputation Power: 6991
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> 
__________________
PHP FAQ
How to program a basic, secure login system using PHP

Quote:
Originally Posted by Spad
Ah USB, the only rectangular connector where you have to make 3 attempts before you get it the right way around

Reply With Quote
  #4  
Old November 12th, 2012, 12:15 AM
Jacques1's Avatar
Jacques1 Jacques1 is online now
pollyanna
Click here for more information.
 
Join Date: Jul 2012
Location: Germany
Posts: 1,838 Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level)Jacques1 User rank is Lieutenant General (80000 - 90000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 1 Day 6 h 6 m 58 sec
Reputation Power: 813
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 ...

Reply With Quote
  #5  
Old November 12th, 2012, 08:39 AM
piperpam27 piperpam27 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2012
Posts: 47 piperpam27 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
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

Reply With Quote
  #6  
Old November 12th, 2012, 01:14 PM
msteudel's Avatar
msteudel msteudel is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Sep 2002
Location: Seattle, U.S.A.
Posts: 712 msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level)msteudel User rank is Lance Corporal (50 - 100 Reputation Level) 
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>

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPHP Development > PHP-General - Echo page contents of HTML page. Worth it?

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap