[thelist] PHP show first 10 words of MySQL field

SBeam sbeam at syxyz.net
Thu Aug 28 11:00:38 CDT 2003

On Thursday 28 August 2003 05:28 am, Kelly Hallman wrote:
> $x = "The quick brown fox jumps over the lazy ".$test.".";
>    +  concatenation double quotes                     0.3742000000
> Two interesting conclusions; interpolating variables within a double
> quoted string seems to be 3x slower than concatenating. However, it
> doesn't seem that the mere presence of the double quotes impacts the
> processing time significantly, unless they contain strings to interpolate.
> Each method has it's strong points. I find the concatenation method hard
> to read and edit and usually opt for interpolation. However, it's good to
> know that this is an area where you can trim some serious time off by
> using concatenation in an intensive loop that needs optimization.

dont forget sprintf() - I get similar results as your last example using 
$x = sprintf('The quick brown fox jumps over the lazy %s', $test);

Some people see sprintf's and get nervous, but once you get used to them it 
really clears up. Especially for constructing SQL statements, because you can 
do addslashes(), or other processing, on the fly, and use sprintf's 
formatting to massage numbers on the way in, e.g.

$sql = sprintf("UPDATE foo_table 
                SET name = '%s'
                  , expires = '%s'
                  , max_price = %.2f
                WHERE id = %d",
              date("Y-m-d", $time),

Thats an extreme example but you can see all the tricks you can pull. If you 
can't handle lining up your placeholders with your arguments then PHP also 
supports 'argument swapping', see the docs. Personally I find it a lot better 
$sql = 'UPDATE foo_table SET
       name = '.addslashes($_POST['name']).',
       expires = '.date("Y-m-d", $timestamp).', 
       max_price = '.$int_total_price .'
       WHERE id = '.$sess->getId();

which is almost impossible to type correctly, hard to read, and is only about 
6% faster. 

More information about the thelist mailing list