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.
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.
PHP has four types of variable scopes including local, global, static, and function parameters.
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.
PHP has three different variable scopes: local. global. static.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With