NYCPHP Meetup

NYPHP.org

[nycphp-talk] Variables syntax

Tim McEwen php at tmcode.com
Fri Oct 28 16:50:56 EDT 2005


Not sure I agree that Version C will give you a speed improvement.    
I think gluing strings together is more expensive than expanding a  
double quoted string.   Take the following two scripts:


<?
//  test_a.php  - using single quotes
$string='a';
for($i=0; $i<100000; $i++)    $string='A' . $string .  'B';
echo strlen($string);
?>


<?
// test_b.php - using double quotes
$string='a';
for($i=0; $i<100000; $i++)    $string="A{$string}B";
echo strlen($string);
?>


Using AB  produces the following:

ab -n 10 http://localhost/test_a.php
...
Time per request:       28574.142 [ms] (mean)
...

ab -n 10 http://localhost/test_b.php
...
Time per request:       14057.574 [ms] (mean)
...

Seems like the concatenate operation is 2x slower?  So I would vote  
for Version A being the "best practice."

I too have heard the argument that you should use single quotes  
(especially for array element keys) because your code will preform  
better.  Has anyone done any benchmarking on this?  Does using $row 
['column']  really perform that much better than $row["column"]?


-Tim




On Oct 28, 2005, at 4:14 PM, Joseph Crawford wrote:

> It's all based on your coding preference.
>
> Version A works because of the {} it evaluates the variable,  
> however version A would work even without the {}
>
> The reason being is because when using double quotes variables are  
> evaluated before being printed.
>
> Version C is the same type i use because from my understanding  
> using double quotes is bad because it takes longer processing to  
> evaluate the variables when they are injected into a string in that  
> manner, maybe i am wrong and just typing more than i have to.
>
> It all boils down to your coding style, preference.
>
> -- 
> Joseph Crawford Jr.
> Zend Certified Engineer
> Codebowl Solutions, Inc.
> 1-802-671-2021
> codebowl at gmail.com
> _______________________________________________
> New York PHP Talk Mailing List
> AMP Technology
> Supporting Apache, MySQL and PHP
> http://lists.nyphp.org/mailman/listinfo/talk
> http://www.nyphp.org

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.nyphp.org/pipermail/talk/attachments/20051028/1eba7c21/attachment.html>


More information about the talk mailing list