View Full Version : <?php ?> Performance Question

04-24-2006, 10:38 PM
I have been working on a project recently and I decided to go down the simplest route I could think of in order to separate all my PHP code from the HTML, that is, use however many scripts I need to generate the data, then parse that data to a function that just includes a template file (just a php file that is mainly HTML with a few <?php echo ?> in), the output is buffered using the ob_ functions and the resulting html is sent wherever it needs to be, simple.

But I was thinking, is having a php file with loads of <?php ?> tags in bad for performance? I remember someone telling me that each time you open and close the tags it takes a performance hit, is this true? And if so, is it actually a major performance hit, or insignificant?

For example, in these two examples is the first one better for performance (even though it is harder to follow and edit):

echo '<p>My name is '.$name.' and I am '.$age.' years old.</p>';

<p>My name is <?php echo $name ?> and I am <?php echo $age ?> years old.</p>

Thanks for your help,

04-25-2006, 04:28 AM
There is no performance hit, the first part of the php parsing process is tokenizing, this decides what parts of your code are actually PHP code and what parts are HTML etc, this part is done all at once, there is no 'jumping in and out' of php mode etc.

04-25-2006, 08:48 AM
Cool, thanks! :)

04-25-2006, 01:47 PM
I've run a test :)

echo '<p>My name is '.$name.' and I am '.$age.' years old.</p>';

will get you a ~0.001second gain after 10000 iterations. :)
(PHP5.1.2 with Zend Optimizer 3.0)

04-25-2006, 08:25 PM
will get you a ~0.001second gain after 10000 iterations

lol, well, thats that answered :), nice one, cheers

04-26-2006, 03:23 AM
However, to my knowledge, it is best to place very large sets of data outside of PHP tags rather than echoing them.. Ive never actually tested it but it does make sense. Please shoot me if I'm wrong :)

The other thing worth noting is that you can't properly test performance using such a small set of data, as an example:

In an application I am working on for a client, An e-mail template needs to be read from a file and then sent. The file may be need to be read 1 - 20 times, depending. I created a class with a static function that would (when called), open the file file specified, cache it, and return the contents.. If the cache was already present it would return the cached file contents to save opening the file again.

I found that when then file contained less than 20 characters it was actually quicker to read the file multiple times than check to see if the file had been cached and then return the stored data. However when I tested an 800k file, the performance increase was immense, from about 2 seconds reading the file 10 times to 0.15 seconds with the cache.

Sorry to ramble... What i'm basically saying is, you can't judge performance properly with such a small test, however if thats all you need it for then its best to go with whicever method is most cumfortable for you to use.

04-26-2006, 08:28 AM
I've used a small amount of data becouse that is what you usually echo. Echoing a large block of data (markup of this page) was done 10x slower by echoing it out 100 thanby "jumping in and out" of PHP.
Though when the echoed value got stored in a variable it was faster (more than 10x) I'm pretty sure you don't output the exact same code blocks houndreads of times though. By printing it once the gain loss would be ~5*10^-6seconds
Tests show that you should not worry about speed. :)