views:

136

answers:

4

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?

+5  A: 

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

Tom Haigh
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
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
A: 

You dont need 'var'.

Visage
A: 

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

Michael Mior
A: 

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.

scrumpyjack