PHP Constants, Magical and Otherwise
We’ve already talked about PHP variables, now we’re going to look at PHP constants. As the name suggests, once you define a constant, its value doesn’t change, and your script can’t un-define it.
PHP comes with many constants built-in for use in situations that require you to interact with certain built-in functions. Generally, these constants are just integers that have no special significance outside their special cases where they’re needed. Here’s where you can find a complete list of these pre-defined constants. {show website} A few more seem to get added with every new release of PHP, so depending on how much time has passed from when this video was recorded to when you started watching it, there could be any number of new constants added to the language. The best way to find out is to check the manual.
Technically, a constant can hold the value of any scalar type. That means ints or strings, which is what you’ll usually see, and also floats and booleans – no arrays or objects. Also, unlike a variable, constants are always global in scope, so it’s important to make sure that you don’t define the same constant more than once. If you try to define a new constant with the same name as one that already exists, you’re going to trigger a fatal error.
Why might you want a constant? Well, let’s look at what a constant gives you. Here’s a simple example:
define(‘MY_FIRST_CONSTANT’, 33);
Notice I chose to use all upper-case letters for my constant’s name. Much like variables, all constants must begin with either a letter or the underscore character, and may only contain letters, the underscore, and numbers in their labels. The names are also case-sensitive, so technically there is no built-in reason to prevent me from using lower-case letters. However, convention states that constants should have descriptive upper-case names, in order to make it obvious that they’re constants.
Now, as far as this one goes, I just picked a random number out of my head and set it as the value. The most important part about a constant isn’t usually the value so much as the name. By having a meaningful name like this, I can incorporate it into my code’s logic. If I want to control what happens in a particular situation, I can create a constant that describes the situation I’m watching for, test whether or not a variable with that value is present, and control what happens next accordingly.
And since a constant’s name is meaningful to a human, when you see it, it ought to be clear what it’s intended to do. Here’s a practical case:
{show example code}
This particular example uses a constant, passed by some calling script, to determine what test to use in order to compare two values. Since GREATER_THAN is more meaningful than the number 3, constants in this situation make it easier to keep the code organized.
Like I said earlier, you want to make sure you don’t define the same constant more than once. To assist you in that, PHP gives you a function called ‘defined’. Call it on a string, and it will return true if that constant has been defined by that point in the script’s execution, and false if it hasn’t.
{show example code and output}
And if for some reason, you ever need to use a variable to hold the name of a constant, and you want to get that constant’s value, PHP provides a function called ‘constant’. Call it on your variable, and it’ll output the value of the constant it refers to, if any.
{show example code and output}
You’ll encounter constants from time to time as you work with PHP’s library of built-in functions, whether you choose to define your own or not. But some of the most interesting constants built into PHP technically aren’t constants at all. These are the magic constants.
All of the magic constants use a similar syntax – 2 underscore characters, followed by an upper-case name, and then two ending underscores. And they get their name from the fact that they look like constants, but their values depend on the contexts in which they’re used.
Here’s the first one:
__LINE__
I can place this magic constant into my script, and wherever it is, that line number, that’s its value. Check it out:
{show example code}
{show output}
Pretty cool, huh? Practically, the most common use of __LINE__ is in debugging. There typically isn’t much call for revealing what line number in your script you’ve reached when you’re in a production environment. But when you’re looking for a bug, it can be very helpful to see how far you’ve gone.
The next magic constant is __FILE__. This time, it takes on the value of whichever file I place it in:
{show example code}
{show output}
And see if I rename the file, like so:
{show output}
It takes on the new filename. Neat, huh? Once again, mainly a tool for debugging.
If what you need is the directory of the current file, you can use __DIR__. This is a new addition to PHP 5.3. For older PHP scripts, you’ll often see:
dirname(__FILE__)
which has the exact same value. I guess the PHP engineers just decided it was time to simplify things. Typically you use it for things like searching a directory or adding a value to the include path.
There’s also a magic constant to get the name of the function it resides in, assuming it resides in one:
__FUNCTION__
I know about this constant, but I’ve never used it for anything, and I’m not entirely sure when it could be useful. Still, there it is.
The last 3 magic constants that exist as of this video deal with topics we haven’t covered yet. But here’s the PHP manual page that talks about them.
{show website}
To find it for yourself, just go to PHP.net and search the online documentation for “magic constants”.
February 09 2010 07:04 pm | Basics and PHP Programming Basics and PHP tutorial scripts
2 Responses to “PHP Constants, Magical and Otherwise”
Dave on 10 Feb 2010 at 9:14 am #
…?
{show example code}
{show output}
{show website}
…?
Wolfman on 10 Feb 2010 at 9:37 am #
Well, these posts are actually video transcripts. I’m going to be recording them as screen captures, and those braces tell me where to show certain things. The words are mostly just what I’ll say while recording.