Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scope of DEFINE variable in php

Tags:

variables

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.

like image 394
Ford Avatar asked Jan 07 '15 02:01

Ford


People also ask

What are the scope of variable in PHP?

In PHP, variables can be declared anywhere in the script. The scope of a variable is the part of the script where the variable can be referenced/used.

What are the 4 types of variable scopes?

PHP has four types of variable scopes including local, global, static, and function parameters.

What does the scope of a variable defined?

In simple terms, scope of a variable is its lifetime in the program. This means that the scope of a variable is the block of code in the entire program where the variable is declared, used, and can be modified.

How many scope of variable is available in PHP?

PHP has three different variable scopes: local. global. static.


2 Answers

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.

like image 102
Salvador Dali Avatar answered Oct 02 '22 03:10

Salvador Dali


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

like image 37
mopo922 Avatar answered Oct 02 '22 03:10

mopo922