php whole source?

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
ZachsCompServ
Forum Newbie
Posts: 15
Joined: Thu Aug 26, 2010 11:29 pm

php whole source?

Post by ZachsCompServ »

Been using PHP for about a year now and still learning. Had a question about programming with php though...

Is it better to just do the whole source with PHP and using "echo" for html codes? Or is it the "thing to do" now to only php certain parts that you need interactivity for, and use html without the php's echo command for the static parts of the page? I hope this makes sense.
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: php whole source?

Post by Jonah Bron »

It's not a good idea to put everything in echos for a couple of reasons:
  1. It uglys up the code
  2. Performance. Anything outside of the <?php ?> tags is not processed by PHP, so there's less load that way
If your wanting to get fancy, you can put all of your code in one file, and all the HTML in another file like this:

index.php

Code: Select all

<?php
//do something cool
$title = 'Viewing item #' . $_GET['product_id'];
$desc = getDescription($_GET['product_id']); // some function to access the db

include('template.html');
template.html

Code: Select all

<html>
    <head>
        <title><?php echo $title; ?></title>
    </head>
    <body>
    <h2><?php echo $title; ?></h2>
    <h5>Description</h5>
    <p><?php echo $desc; ?></p>
    </body>
</html
If you want to get really fancy, check out the Model-View-Controller framework (MVC)
http://en.wikipedia.org/wiki/Model%E2%8 ... Controller
ZachsCompServ
Forum Newbie
Posts: 15
Joined: Thu Aug 26, 2010 11:29 pm

Re: php whole source?

Post by ZachsCompServ »

I figured it would be better to do php only when needed. Thanks. I'll check out the other suggestions as you mentioned later on.
Post Reply