Ford
Ford

Reputation: 567

Scope of DEFINE variable in php

Just a quick questions please, that I can't find the answer to.

If I define a variable like the below example:

DEFINE('THIS_TEST', 'ABC');

What is the scope of this? Could I then use this with in a class/object function:

public function testFunction() {
    echo THIS_TEST;
} 

I have tried this in something similar but am not getting the results I was expecting, although this could be related to other issues.

Any advice would be appreciated.

Upvotes: 6

Views: 3747

Answers (2)

Salvador Dali
Salvador Dali

Reputation: 222701

You can read about scoping here.

Like superglobals, the scope of a constant is global. You can access constants anywhere in your script without regard to scope. For more information on scope, read the manual section on variable scope.

Upvotes: 4

mopo922
mopo922

Reputation: 6381

Assuming you actually mean the lowercase define(), this defines a constant (not a variable), which is available globally (with the exception of namespaces):

http://php.net/manual/en/function.define.php

Upvotes: 3

Related Questions