0

I've created a small MVC framework in PHP. Pages are created dynamically, and are then returned to the client/browser.

At this moment I create the entire file (HTML output) in the variable $content.

Next, I write this content in a temporary file, and require this file, thus returning the content to the client.

Now, I was wondering... Can't I just send it from memory, i.e. just echo-ing the variable:

<?php ... echo $content; ... ?> 

Is this effectively the same as writing it to a temp file and then requiring/including it, only faster? Or do you have any better tips?

2
  • I think you will find it to be slow to serve pages by building up the page content in a variable before echoing it out. I'm hoping you're writing to a file so that you can serve static cached content without the rebuilding step. Commented Sep 21, 2012 at 15:13
  • The point of require() is to be able to include more PHP code, and organise your project in a more logical way with multiple files. It is not intended to send output that way, that's what echo is for. If you would still like to send an entire file as content, you'd be better off with readfile() -- but be aware that any PHP code won't be processed. Commented Sep 21, 2012 at 15:50

1 Answer 1

5

Yes, you can absolutely just echo it out.

The value in storing them in temporary files would be for instances where you an just require that same temp file instead of doing all of your heavy lifting all over again (logic, db calls, etc)

Sign up to request clarification or add additional context in comments.

4 Comments

+1 Also you may want to consider NOT storing it all in one variable and just echoing it out as you go along. This is the standard practice. Is there any benefit to storing it all in one variable then echoing it at once?
@anonymousdownvotingislame: Yes there is. Because you can do stuff with it. If it's HTML, you can DOM manipulate it for example.
@MadaraUchiha Why don't more CMSs do this then. Seems logical to have a page variable easyily hackable. Maybe that's why not.
@anonymousdownvotingislame Thanks for your input! what would make it less hackable? Split the header, body and footer in three variables for example? Or do you have other ideas on how to tackle this.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.