Page 1 of 1
php whole source?
Posted: Mon Sep 13, 2010 8:02 pm
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.
Re: php whole source?
Posted: Mon Sep 13, 2010 8:28 pm
by Jonah Bron
It's not a good idea to put everything in echos for a couple of reasons:
- It uglys up the code
- 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
Re: php whole source?
Posted: Mon Sep 13, 2010 8:37 pm
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.