Forum Moderators: coopster
There are 2 ways to write your code:
1. To write php code through html context
2. Create the whole website writing php commands
The author said that the second option was generally the most maintainable, and I wanted a few opinions from other developers.
I am still in the process of creating many scripts for my own websites and I would like to have the code more maintainable, but I also need a good deal of time.
what is the best way to code php applications?
Also when writing programs split as often as possible long code pages into smaller functions that can be reused
It will make your life easier and code more maintainable and readable
Of course use many white space and tons of // comments
Another good reason to deal with small chunks of code lies in easy debugging and testing
Book: Great OOP and template learning: "PHP Advanced"
<edit: Added a few items />
<?php
do_header();
echo '
<!--printstart-->';include('install.htm');
echo '
<!--printend-->';
include('stuff/sig.php');
require('stuff/menu.php');
require($_SERVER['DOCUMENT_ROOT'].'/inc/comments.php');
require('stuff/footer.php');
echo '
</body></html>';
...
what's missing, of course, is any actual code! (i did say "simple") and the lots and lots of comments that would go with it.
mixing html and php in the same documents is fine for small, simple things; being able to drop in and out of html is one of php's coolest features, but for more complex stuff, "web applications", you definitely want to keep things as seperated and modularised as possible.
often this isn't possible. somtimes you need to put it all in one file, but that doesn't mean it can't still be modularised, seperated..
function intro_text() {
echo'
this is the intro text';
}
and I'm a big fan of one-page drop-in php systems, things like the excellent Qdig. it all depends on what you are trying to achieve. most coders spend waaay to little time at the planning stage.
at the end of the day, one-size doesn't fit all, and after considering your site, and what you need it to do, and how; you will likely arrive at any number of solutions. planning, with the users in mind, is the key.
By users, I do also mean the folks that will have to maintain the content and code, weeks and months down the line. Put yourself in a complete strangers shoes, looking at your code one year from now and having to customise/alter the site code. this stranger is you, long after you've forgotten why you did a particular thing a particular way.
/* comments! comments! comments! */
An easily mainainable site will be a joy to work on, and that will show in the site itself.
;o)
(or