SitePoint Sponsor

User Tag List

Results 1 to 14 of 14
  1. #1
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Very basic question about include files

    Hi,

    I'm working my way through the 'Build Your Own Database Driven Web Site Using PHP & MySQL' and I have a very basic question about includes.

    My background until now has been in static html programming and I think this is causing me to get a bit confused.

    In chapter 3 of the book (around p109), a basically html page called count.html.php (with a minimal amount of php contained therein) is included in the entirely php page called index.php.

    I'm wondering why it's not the other way around, i.e. setting up the html page as the index and then using an include to drag all the php code into it.

    Can someone enlighten me please?

    Thanks

    Ian

  2. #2
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Since HTML files don't get parsed by PHP, you can not include files in HTML files (since include() is a PHP function).
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  3. #3
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the reply, but as I said in my original post the "basically" html file is called count.html.php and therefore *does* get parsed by the PHP engine. It contains some php code, which obviously wouldn't get parsedwithout the .php file extension.

  4. #4
    Utopia, Inc. silver trophy
    ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    9,097
    Mentioned
    153 Post(s)
    Tagged
    2 Thread(s)
    Well than I guess it is because index.php is the default file that is served upon requesting / on a domain or directory.

    Why they dont use count.html.php as index.php I don't know, and I don't have the book, so I really can't explain it all that well I suppose
    Rémon - Hosting Advisor

    SitePoint forums will switch to Discourse soon! Make sure you're ready for it!

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  5. #5
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy

    Quote Originally Posted by ScallioXTX View Post
    Well than I guess it is because index.php is the default file that is served upon requesting / on a domain or directory.

    Why they dont use count.html.php as index.php I don't know, and I don't have the book, so I really can't explain it all that well I suppose
    I understand why index.php is served as default.

    My question is why index.php doesn't contain the bulk of the html, with the necessary php code as includes rather than the other way round. Perhaps I've missed a basic point about controllers.

    If anyone, who has the book, can explain, that would be very helpful.

    Thanks

  6. #6
    SitePoint Wizard bronze trophy cydewaze's Avatar
    Join Date
    Jan 2006
    Location
    Merry Land, USA
    Posts
    1,096
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I have that book, but my page 109 is different than yours. Perhaps we have different editions? I have the 4th edition.

  7. #7
    John 8:24 JREAM's Avatar
    Join Date
    Sep 2007
    Location
    Florida
    Posts
    1,508
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey there, I don't have the book but here's what I think..

    index.php -- You want to use more includes for things that you will re-use, say like a function.

    If you had a function created an HTML list (lets say by doing a 'for loop'), then you could type one line in index.php and have it automatically insert all that HTML.

    Then if you had like about.php, you could include the file again, and type one line of code to repeat the entire generated HTML list again.

    It's basically about code re-use, and it is probably considering that you will use more than one page.

    It's also good because if you organize your functions and operations, its easy to transfer all those files to a new directory for a fresh new project. Rather than weening through a bunch of index.php-like files and cleaning out what you want and don't want.

  8. #8
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm using the sample pdf download of the 4th edition and count.html.php appears on page 108. Maybe it's different in the print version.

  9. #9
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JREAM View Post

    It's basically about code re-use, and it is probably considering that you will use more than one page.
    JREAM, thanks for the reply. I completely understand the principle of using includes in order to be able to re-use code efficiently.

    I'm obviously not phrasing my question correctly. I'll think about it some more and see if I can explain myself better.

  10. #10
    SitePoint Wizard
    Join Date
    Mar 2008
    Posts
    1,149
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I assume that it is a simple way to use template files. Separating the "view" from the "logic."

  11. #11
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    The included .html.php file in this case is acting as a template file. The advantage of this is that it can be edited easily by a designer, or you can switch and use a different template file if you wanted a slightly different layout. That would be a lot harder to do if the controller contained most of the html, as it were.

  12. #12
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Stormrider, thanks, that makes total sense. Thanks for persevering with me. Much appreciated.

  13. #13
    SitePoint Zealot Kayarc's Avatar
    Join Date
    Sep 2009
    Posts
    127
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by gotorightway123 View Post
    hanks for the reply, but as I said in my original post the "basically" html file is called count.html.php and therefore *does* get parsed by the PHP engine. It contains some php code, which obviously wouldn't get parsedwithout the .php file extension.
    You did not make the original post here unless you are running two separate accounts.
    Phoenix Arizona Web Design | info *at* kayarc.com | 602.633.2676

  14. #14
    SitePoint Member
    Join Date
    Aug 2009
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I only have one account. I have no idea what gotorightway123 is up to.


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •