Page 1 of 1

puzzling parse error

Posted: Sat Apr 25, 2009 9:47 am
by oboedrew
I'm puzzled by a parse error.

This works:

Code: Select all

$NewVariable='[BBCodeTag]'.$OldVariable.'[/BBCodeTag]';
But this gives me a parse error:

Code: Select all

$NewVariable="[BBCodeTag]$OldVariable[/BBCodeTag]";
Can anyone explain this?

Thanks,
Drew

Re: puzzling parse error

Posted: Sat Apr 25, 2009 11:54 am
by Benjamin
What is the error? This should work:

Code: Select all

 
$NewVariable = "[BBCodeTag]{$OldVariable}[/BBCodeTag]";
 

Re: puzzling parse error

Posted: Sat Apr 25, 2009 2:36 pm
by McInfo
When the PHP parser sees the square bracket ([) after the variable, it thinks it is the beginning of a reference to an array element.

Edit: This post was recovered from search engine cache.

Re: puzzling parse error

Posted: Sat Apr 25, 2009 4:07 pm
by MasterBeta
When using double quotes PHP will parse the information within.
When using single quotes PHP will NOT parse the text.

Re: puzzling parse error

Posted: Sat Apr 25, 2009 4:29 pm
by oboedrew
McInfo wrote:When the PHP parser sees the square bracket ([) after the variable, it thinks it is the beginning of a reference to an array element.
Ah, that makes sense. Can't believe I didn't realize that.
astions wrote:$NewVariable = "[BBCodeTag]{$OldVariable}[/BBCodeTag]";
I will give that a try. I'm curious, which of the following is considered better coding practice?

Code: Select all

$NewVariable = "[BBCodeTag]{$OldVariable}[/BBCodeTag]";
or

Code: Select all

$NewVariable='[BBCodeTag]'.$OldVariable.'[/BBCodeTag]';
Is there any reason to use one over the other? If so, why? Or are both considered equally good style?

Thanks again, guys. This forum continues to be an invaluable resource whenever I'm stumped.

Cheers,
Drew

Re: puzzling parse error

Posted: Sat Apr 25, 2009 6:20 pm
by McInfo
Mostly, it's a matter of personal preference and readability.

I wrote a script to see if there is a speed difference between the different styles. I ran speed-test.php 20 times for each case. Take the results for what you will. Concatenation appears to have a slight speed advantage.

speed-test.php

Code: Select all

<?php
header('Content-Type: text/plain');
if (isset($_GET['case']) && $_GET['case'] > 0 && $_GET['case'] < 5)
{
    $case = $_GET['case'];
    $str = '';
    $loops = 100000;
    switch ($case)
    {
        case 1 :
            $start = microtime(true);
            for ($i = 0; $i < $loops; $i++)
            {
                $str .= 'BEGIN-'.$i.'-END';
            }
            $total = microtime(true) - $start;
            break;
        case 2 :
            $start = microtime(true);
            for ($i = 0; $i < $loops; $i++)
            {
                $str .= "BEGIN-$i-END";
            }
            $total = microtime(true) - $start;
            break;
        case 3 :
            $start = microtime(true);
            for ($i = 0; $i < $loops; $i++)
            {
                $str .= "BEGIN-{$i}-END";
            }
            $total = microtime(true) - $start;
            break;
        case 4 :
            $start = microtime(true);
            for ($i = 0; $i < $loops; $i++)
            {
                $str .= "BEGIN-".$i."-END";
            }
            $total = microtime(true) - $start;
            break;
    }
    echo 'Total time: '.$total.' seconds';
    if ($fh = fopen('speed-test-log-'.$case.'.txt', 'a'))
    {
        fwrite($fh, $total."\n", 20);
        fclose($fh);
    }
}
else
    echo 'Set a case. Example: speed-test.php?case=1';
?>
calc-averages.php

Code: Select all

<?php
header('Content-Type: text/plain');
for ($c = 1; $c < 5; $c++)
{
    $file = "speed-test-log-{$c}.txt";
    if (file_exists($file))
    {
        $times = file($file);
        echo "Case {$c} Average Time: ".(array_sum($times) / count($times))." seconds\n";
    }
    else
        echo "No case {$c} log.\n";
}
?>
Results from calc-averages.php

Code: Select all

Case 1 Average Time: 0.112371003628 seconds
Case 2 Average Time: 0.122534871101 seconds
Case 3 Average Time: 0.123497724533 seconds
Case 4 Average Time: 0.113371384144 seconds
Edit: This post was recovered from search engine cache.