Re: use of curly braces with string interpolation.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Jochem Maas wrote:
hi guys,

[[
I original posted this to php-internals because I figured that php-general would not know the answer _ it had to do with intended use of functionality (I couldn't find anything on the web about) and I figured that the guys who wrote/write php would be best equipped to answer. well no joy - the post was gloriously ignored, so I figured I give it a shot here...
]]


I always use curly braces around vars placed in double quoted strings, I do this for 2 reasons:

1. I believe It helps the engine because the var delimitation is explicit, and this means the interpolation is (should be?) faster.
it's just as fast as not using it. The delimiter with variables is simply the $[a-zA-Z_\x7f-\xff][a-zA-Z0-9_\x7f-\xff]* part. Simple variables (scalar) are described using the above, it doesn't make it faster to put more around em. Adding {} around array's/objects shouldn't make it faster, since it's still just taking the entire thing as a variable until it comes acress a character that either delimits the whole thing, or is simply not allowed there.

2. it looks nice (easier to read!)
I think it looks ugly... but it's a simple matter of personal taste ;)

I have tried to test this with a simple script that does a million interations - but I cannot get it to return consistent results - one time with curlies is faster, another its slower. the (php5) script I used to test is included at the bottom of the email for those who are curious. (I haven't been able to find relevant info on either google or php.net - are there any other search engines? ;-)

I am interested in knowing whether my first assumption regarding speed of interpolation is (in theory) at all correct.

If any of the php gurus would give advice as to use of curly braces in interpolated strings would it be:

a, always use them (its faster/better)
b, only when absolutely necessary
c, go away troll and figure it out for yourself
    (this one is tempting, n'est pas ;-)
d, something I haven't had the presence of mind to think of!

[[
choice c, is only relevant to internal btw :-)
]]

kind regards,
Jochem.

<?

class Tester
{
    public $testvar;
}

$t = new Tester;
$t->testvar = 'superduper';
$a = array('junk' => 'notbad!');

var_dump($t, $a);

$strs = array();

$start = mktime();
$i = 0;
do {
    $c = 'simplicity' + $i;
    $strs[] = "{$t->testvar} yadda yadda {$a['junk']} - {$c}\n";
    $i++;
} while ($i < 1000001);

$diff = (mktime() - $start);
echo "With Curlies - Total Time Taken ($diff): ".floor($diff / 60).' mins & '.($diff % 60).' secs'."\n";


$strs = array();

$start = mktime();
$i = 0;
do {
    $c = 'simplicity' + $i;
    $strs[] = "$t->testvar yadda yadda $a[junk] - $c\n";
    $i++;
} while ($i < 1000001);

$diff = (mktime() - $start);
echo "Without Curlies - Total Time Taken ($diff): ".floor($diff / 60).' mins & '.($diff % 60).' secs'."\n";

-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux