3

I'm getting this error: "PHP Parse error: syntax error, unexpected T_VARIABLE in /var/www/vhosts/... on line 66"

Here's my code:

function combine($charArr, $k) {

    $currentsize = sizeof($charArr);
    static $combs = array();
    static $originalsize = $currentsize; ###### <-- LINE 66 ######
    static $firstcall = true;

    if ($originalsize >= $k) {

        # Get the First Combination 
        $comb = '';
        if ($firstcall) { //if this is first call
            for ($i = $originalsize-$k; $i < $originalsize; $i++) {
                $comb .= $charArr[$i];
            }
            $combs[] = $comb; //append the first combo to the output array
            $firstcall = false; //we only want to do this during the first iteration
        }
    ....
    ....
}

Any idea what's wrong?

trusktr
  • 44,284
  • 53
  • 191
  • 263

4 Answers4

7

Quoting the manual (that page is about static properties, but the same applies for variables) :

Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

You are using this :

static $originalsize = $currentsize;

Which is initializing with an expression -- and not a constant.


And here's the manual's section that says quite the same about static variables :

Static variables may be declared as seen in the examples above. Trying to assign values to these variables which are the result of expressions will cause a parse error.

And, just in case, here's about expressions.


In your case, to avoid that problem, I suppose you could modify your code, so it looks like this :

$currentsize = sizeof($charArr);
static $originalsize = null;
if ($originalsize === null) {
    $originalsize = $currentsize;
}

With that :

  • The static variable is initialized with a constant
  • If its value is the constant one, assign the dynamic value.
Pascal MARTIN
  • 395,085
  • 80
  • 655
  • 663
  • Thanks Pascal, excellent answer! You solution helps me retain the value of $originalsize from whatever it was set the first time the function was called. :) – trusktr Feb 25 '11 at 21:42
  • That condition will return true on every test. Don't you think you should change static $originalsize = null; for static $originalsize; and perhaps test the value of $currentsize instead? – dabito Feb 25 '11 at 22:29
  • dabito, $currentsize changes with each call of the function (it's recursive). $originalsize needs to be the same for always, starting with the first value it gets set to in that conditional statement. – trusktr Feb 27 '11 at 08:21
3
static $originalsize = $currentsize; ###### <-- LINE 66 ######

You can't pass a variable as the default value of a static variable. Instead, do the following:

static $originalsize;
$originalsize = $currentsize;
Bruno De Barros
  • 1,535
  • 2
  • 16
  • 30
0

To quote the php manual:

Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

jeroen
  • 91,079
  • 21
  • 114
  • 132
0

From php manual:

Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

Czechnology
  • 14,832
  • 10
  • 62
  • 88