i would like to know which one is the best for performance:
1- use smarty template ( or any other better)
<?php
$Smarty = new Smarty();
// set all the default variables and other config
$Smarty->assign('var1', 'hello');
$Smarty->assign('var2', 'world');
$Smarty->display('page.html');
2- use this code:
<?php
$var1 = 'hello';
$var2 = 'world';
echo "$var1 $var2";
3- use this code:
<?php
$var1 = 'hello';
$var2 = 'world';
echo $var1 . " " . $var2;
based on these 3 examples i cant think about a new one, which one is best to use when performance
note: of course i have more variables than this example.
thanks
As far as I remember, concatenating PHP variables (as in your third example) is faster than using
"$var1 $var2"given a mix of variables and constant strings as each token is evaluated on the fly (which is bad).So, between 2 and 3, I believe it depends on context: If you have a long string with a mix of variables and constants, then method 3 would be faster. Otherwise, if it’s identical to your example, 2 might be faster (however, the difference is negligible and therefore should be a moot point).
Using a templating engine will always be slower than raw code.
Now if you don’t have a very good reason to not use a templating engine, you should by all accounts use one. Why?