puzzling parse error

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
oboedrew
Forum Commoner
Posts: 78
Joined: Fri Feb 20, 2009 1:17 pm

puzzling parse error

Post 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
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: puzzling parse error

Post by Benjamin »

What is the error? This should work:

Code: Select all

 
$NewVariable = "[BBCodeTag]{$OldVariable}[/BBCodeTag]";
 
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: puzzling parse error

Post 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.
Last edited by McInfo on Tue Jun 15, 2010 12:18 pm, edited 1 time in total.
MasterBeta
Forum Commoner
Posts: 38
Joined: Thu Apr 02, 2009 4:35 am
Location: Lincoln, NE

Re: puzzling parse error

Post by MasterBeta »

When using double quotes PHP will parse the information within.
When using single quotes PHP will NOT parse the text.
oboedrew
Forum Commoner
Posts: 78
Joined: Fri Feb 20, 2009 1:17 pm

Re: puzzling parse error

Post 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
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: puzzling parse error

Post 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.
Post Reply