Since this keeps coming up, I thought I would post this. I've hesitated in the past since it's such a huge block of text, but here it is. There are four tests here.
Test 1. Assign simple text strings to variables. No output.
Test 2. Assign text strings with variables to variables. No output.
Test 3. Output strings with variables, using fastest method from Test 2.
Test 4. Output huge blocks of text.
Tests all include three parts
1. Description of the test
- All results sorted from slowest to fastest, so first is worst.
- Results are given in seconds
- Times are those returned from XDebug profiler
- I ran most of these tests several times to verify that these results are representative. To the extent that they vary with multiple tests, that is noted in the "interpretation" section.
For Tests 1 and 2, the function names tell how the text is being assigned.
heredoc_* use heredoc
accum_* use .= on each line
concat_* use concatenate with . to begin each line and one single equals
*_sq_* use single quotes
*_dq_* use double quotes
*_sub_* use variable substitution within the string
*_concat_* use concatenation within the string
Test 1: assigning simple strings.
- no variable substition.
- 20 lines of 60 characters of plain text assigned to a variable with a function
- 100 iterations within each function
- each function called one time.
- heredoc is an order of magnitude slower than other methods.
- sq is slightly faster than dq
- concatenation is the fastest
Test2: assigning strings with variables in them
- same functions: 20 lines, 60chars, 100 iterations
- each function called 5 times
- each line has the same string substituted at three points
- string changes with each function call.
- times are average of five function calls.
*There's more variation with this one and accum_dq_var_sub and concat_dq_var_sub trade places for slowest.
*The two fastest also trade places.
*Obviously double-quote variable substitution makes the processor work hard and concatenation roughly 3x faster than using
Test 3: echoing strings with variables
- since accum_dq_var_concat was fastest, we use that as the base.
- same output, but this time we try four possibilities
1. echo each line and use concatenation to construct the line
2+3. switch to html and back to php each time there is a variable to output, using shorthand (i.e. <?=$var;?>) and echo (<?echo $var;?>).
4. accumulate strings to a variable and then echo $var at the end (thus only one echo statement).
- call each function 15 times. Each function prints the 20 lines of text 100 times
* Lots of variability here. Any of the three slowest methods can change places and are usually close to each other (+ or - 20%).
* accumulating all output to a variable and then echoing once is way faster. Always at least an order of magnitude.
Test 4: huge chunks of text, no variables
- this basically tests how much extra overhead is incurred by running everything through PHP.
- five iterations outputting about a 2MB string
**Assign string to var then echo var
Opcode Compiling: 24.6104161739
Total Execution: 7.4180238247
Total Processing: 32.0284399986
**Leave php on each iteration
Opcode Compiling: 0.2842299938
Total Execution: 29.7363541126
Total Processing: 30.0205841064
**echo <<<heredoc string
Opcode Compiling: 2.4830060005
Total Execution: 27.4329669476
Total Processing: 29.9159729481
Only 10% variation and this is about the best possible case for exiting php. The advantages of stepping out of php seem to be minimal for large strings and the disadvantages substantial for shorter strings. Therefore, you never lost much time by echoing, but you can by stepping in and out of PHP.