Everything PHP: Constants and Globals

Published on Mar 24, 2009   //  Development
Off

Everything PHP

We have previously covered variables, which allow you to store and change data, and today we’re going to be talking about something related: global variables and constants.

Global Variables

When you create a variable, it is only available within the same scope. Which means that when you declare a variable in your script, that variable will only be available within the same scope. The scope is hard to example without examples, so let’s look at a few.

[code='php']$a = 26;
echo $a;[/code]

This works because $a is within the scope of echo.

[code='php']$a = 26;
function test_example() {
echo $a;
}
test_example();[/code]

This will not work because you’re trying to get $a from a different scope. Additionally, any variables declared within a function will not be available (or modify any) outside of that function’s scope. Basically outside of a function variables are in the global scope, and inside of a function, variables are in the local scope.

But, what if we want to use a variable from a different scope? Well, that’s where global variables come in to play. We can take a variable from a different scope and bring it into the current scope. This is done by declaring the variable(s) we want as a global. Let’s look at our last example again, but this time let’s make it work.

[code='php']$a = 26;
function test_example() {
global $a;
echo $a;
}
test_example();[/code]

Now that we have globaled the variable $a in the local scope, we now are free to use it and modify it (which will modify globally). We can global multiple variables in the same statement by separating them by commas. Alternatively to globalling a variable, we can use the form $GLOBALS['a'] in place of $a in our local scope.

Constants

A constant is similar to a variable, in that stores a value for retrieval later in your script, however there are a couple of major differences. First of all, once the initial value of a constant is set (when it’s defined), the value cannot be changed (hence the name constant). Also, constants are automatically available as a global. Constants are defined and referenced in a different way than variables. Let’s look at an example.

[code='php']define('THE_CONSTANT', 'bacon');
echo THE_CONSTANT;[/code]

In the define statement, the first parameter is the name of the constant (should always be uppercase, must start with a letter or underscore and may contain letters, numbers and underscores) and the second parameter is the value of the constant (can be a boolean, integer, float or string). That constant is now defined, available globally and it’s value cannot be changed. It is not possible to undefine a constant.

When referencing a constant in a statement, you can simply use the constant name as it was defined. If you do not know the name of the constant (such as if the name is stored in a variable), you can use the function constant() to reference it (ex: echo constant(‘THE_CONSTANT’);).

If you need to check if a constant has been defined, you can use the defined() function in an if statement (it will return true or false).

Conclusion

That finalizes our post on global variables and constants.