Forum Moderators: coopster
Until now I've been breaking out of php for most html stuff like body tags and so on and using php for content areas.
Somewhat like <title><?php echo $variable;?></title> and so on.
would it be better to stay in php and stick the html into echo statements? Overall there isn't a lot of html on many of the pages since all the content is pulled from a database.
Is there a speed difference? memory efficiency issue? anything I should be aware of? I'm thinking of sticking it all into echo statements and staying within php so I can do something like send a 404 header if the content is no longer available from the database rather than just give a blank page. (should see my goof up with pagination and no content and the never ending "next page" link heh)
The above example would then be
echo '<title>'.$variable.'</title>';
instead.
If you've large blocks of HTML with no dynamic content, break out. If you find you're breaking in and out too much, stick it in the PHP. Remember you can use simple scalar variables within double-quoted strings:
echo "<title>$title</title>";
...and complex vars within {}:
echo "<title>{$arr['title']}</title>";
Also consider heredoc syntax which is a sort of best of both worlds - see [php.net...]
[webmasterworld.com...]
[webmasterworld.com...]
using the concatenate operator
$completed_order = 'hallo ';
$completed_order .= 'mr. smith, ';
$completed_order .= 'your order is complete';
echo $completed_order;
i do this all the time as it is a good way of injecting vars into templates which are stored elswhere. e.g. the page is generated, all the vars are assigned and just before it is echoed to the screen all the vars are injected into the template which looks like:
<html>
<head>
<title>{$page_title}</title>
</head>
<body>
{$completed_order}
</body>
helps keep templates separate from php code, which i prefer. there are some great articles on this sort of thing if you google for 'php template theory'
An exception I allow in my code is for long, processor-intensive processes that take 8 seconds or more. In such cases I print() little status messages and use flush() during the loop so I can actually watch the script progress. Sometimes it's as simple as writing a "." and flushing it so you can see the loop progress:
start .......................... done
also clean up my code and learn Objects sometime.. I see glimpes of how it works and I like.
-- widget.php --
include( 'setup.php' ); // set up compression, functions, db connection and so on
include( 'widget_setup.php' ); // functions and calculations just for widget page
include( 'header.php' ); // page layout, no functions or calculations
include( 'widget.php' ); // page layout, no functions or calculations
include( 'footer.php' ); // page layout, no functions or calculations
This setup helps keep your scripts seperated. Like httpwebwitch said, "think first, speak second".
If you keep your calculations in seperate pages from your layout pages it makes your scripts much easier to read and edit. If you use a method similiar to this at least if you choose to use html with php code inside the html it will just be layout. Your page wont have functions and calculations mixed in with the html.