[thelist] PHP show first 10 words of MySQL field

Kelly Hallman khallman at wrack.org
Thu Aug 28 04:28:33 CDT 2003

iOn Thu, 28 Aug 2003, Paul Bennett wrote:
> >I have now got it working, and very simple it is to;
> >$rest = substr("$myrow[text]", 0, 180);
> >      echo("$rest");
> may I make a PHP programming suggestion? Using variables insided double
> quotes is making the PHP engine do more work than it needs to, as it
> needs to scan the string before processing it to see if there are any
> variables inside to parse.

I just did some quick benchmarks on this, and the results were
interesting. I doubted that using string/variable interpolation really 
made this much of a difference (times are seconds, 50000 loops each):

$x = $test;
   +  variable assignment                             0.2610000000

$x = "The quick brown fox jumps over the lazy $test.";
   +  normal interpolation                            1.1313000000

$x = "The quick brown fox jumps over the lazy {$test}.";
   +  curly interpolation                             1.1362000000

$x = 'The quick brown fox jumps over the lazy '.$test.'.';
   +  concatenation                                   0.3758000000

$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.

> It is good practice to get into the habit of doing something like:
> echo 'here is some text with a '.$variable.' in the middle';
> less processor work, and easier to debug IMHO

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.

Kelly Hallman

More information about the thelist mailing list