Monday, 25 September 2017

php - I'm getting a "syntax error, unexpected T_VARIABLE" error. I don't see what I'm doing wrong?

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.

No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...