vote up 0 vote down star

I have the following constant defined in my PHP script:

define("MODULE_PATH",   "D:\\modules\\");

Whenever I try and assign the constant to a variable or as a function argument, I get a PHP parse error (with no explanation).

var $jim = MODULE_PATH;
var $fh  = fopen(MODULE_PATH . "module1.xml");

Both above lines throw the parse error. I even tried using a variable instead of a constant and the error is still thrown. If I just echo the constant, it works fine but any assignment of the constant throws the parse error.

I'm almost to the point of tearing my hair out! Anyone know what the problem is here?

flag

4 Answers

vote up 5 vote down check

You don't need the var keyword if you are setting a local variable - this will work:

$jim = MODULE_PATH;
$fh  = fopen(MODULE_PATH . "module1.xml");

The var keyword is used for class instance variables in PHP4 (still supported in PHP5, alias for public) , e.g.

class a {
    var $b = 'hello';
}

However, if you were declaring an instance variable, it wouldn't work because:

The default value must be a constant expression, not (for example) a variable, a class member or a function call.

http://php.net/manual/en/language.oop5.basic.php

link|flag
It's worth pointing out that in PHP 5.2 and upwards, "var" is a synonym for "public". In PHP 5.0 and 5.1, it's deprecated - it was brought back due to revolts, I imagine. – Samir Talwar Jun 16 at 13:30
Well I'll be. I thought I'd done that but I must have left some more erroneous code in. I'm a javascript developer so I automatically put the var in when I'm writing PHP since the syntax is similar. Thanks! – Andy E Jun 16 at 14:24
vote up 0 vote down

You dont need 'var'.

link|flag
vote up 0 vote down

If you take out var, this works fine for my on PHP v5.2.6. What version are you using?

link|flag
vote up 0 vote down

It looks like you're running on Windows? Try swapping the backslashes for forward slashes... you won't need to do \-escaping, saving some hassle later.

link|flag

Your Answer

Get an OpenID
or

Not the answer you're looking for? Browse other questions tagged or ask your own question.