Forum Moderators: coopster

Message Too Old, No Replies

Breaking out of php for html or no?

any pros or cons?

         

Code Sentinel

10:29 pm on May 13, 2004 (gmt 0)

10+ Year Member



So I'm throwing together scripts using various methods while I learn PHP.

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.

ginga

11:17 pm on May 13, 2004 (gmt 0)

10+ Year Member



Just maintainability and readability really.

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...]

Timotheos

12:02 am on May 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Here's some other threads on this topic

[webmasterworld.com...]
[webmasterworld.com...]

Code Sentinel

4:05 am on May 14, 2004 (gmt 0)

10+ Year Member



Thanks for the links, reading through one of them what exactly does this mean:

"accumulating all output to a variable and then echoing once"

or rather how is it done.. I understand what it means :)

jamie

6:52 am on May 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



hi code sentinel

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'

Timotheos

3:15 pm on May 14, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah I found the thread that is really relevant.
[webmasterworld.com...]

Code Sentinel

9:09 pm on May 14, 2004 (gmt 0)

10+ Year Member



I read up on accumulating and ran across an article on optimizing where they mention using ob_start(); to buffer the output is basically the same thing.

Would I still benefit from accumulation of I use output buffering?

httpwebwitch

3:28 am on May 15, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I like scripts that "think first, speak second". So if any calculations or data processing are required, they are done before writing anything to the browser.

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

WhosAWhata

4:34 am on May 15, 2004 (gmt 0)

10+ Year Member



as a side note, if you decide to stick to just using the PHP to echo content areas, it is easier to put
<?=$variable?>
it will do the same as
<? echo $variable;?>

Code Sentinel

5:06 am on May 15, 2004 (gmt 0)

10+ Year Member



The speed difference seems so minimal.. I think I'll stick with staying within PHP for simplicity. Perhaps one day if I got a load problem. Rather than look at these little issues I figure throw more hardware or some form of caching at it :)

also clean up my code and learn Objects sometime.. I see glimpes of how it works and I like.

corz

10:07 am on May 15, 2004 (gmt 0)

10+ Year Member



it is easier to put
<?=$variable?>

if short open tags are enabled on the server

another way to do it is to use an include..

include ('some.html');

;o)
(or

WhosAWhata

5:52 pm on May 15, 2004 (gmt 0)

10+ Year Member



if short open tags are enabled on the server

yes of course you're right. thanks for the catch.
i often forget to mention the configuration settings since they are set by default on my server...good catch

twist

7:30 pm on May 16, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



My basic page structure,

-- 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.