PHP Constants
Constants in PHP are like variables that you lock with a key and throw away the key — once set, they can’t be changed or undefined. Perfect for values that should never vary during your script’s execution, like version numbers, license keys, or plugin slugs.
A constant starts with a name (letters or underscores only — no $) and holds a value that stays fixed. You create one using the define() function:
define("PLUGIN_NAME", "Pretty Links");
echo PLUGIN_NAME;
That prints “Pretty Links,” and no matter what happens later in your code, PLUGIN_NAME will always be that same string.
You can also use the const keyword, which works similarly:
const COMPANY = "CaseProof";
echo COMPANY;
But there’s a subtle difference:
constcan’t be declared inside functions or conditionals — it’s for top-level, global definitions.define()can be used anywhere, even inside functions or if-blocks, since it’s evaluated at runtime.
PHP 7 introduced something handy — constant arrays.
You can define an array of fixed values that never change:
define("PRODUCTS", [
"MemberPress",
"Pretty Links",
"ThirstyAffiliates"
]);
echo PRODUCTS[0]; // MemberPress
This can be useful for fixed lists like plan tiers or supported plugin names.
And since constants are automatically global, you can access them from anywhere — even inside a function — without having to pass them as parameters:
define("SUPPORT_EMAIL", "support@caseproof.com");
function getSupportContact() {
echo SUPPORT_EMAIL;
}
getSupportContact();
That’s the beauty of constants — they’re predictable, safe, and available everywhere.
Now, magic constants in PHP aren’t spells — but they do change depending on where they’re used. They’re predefined by PHP, written with double underscores on both sides (like __FILE__), and they give you info about your script’s current context — file, class, function, etc.
Think of them as PHP’s built-in “debug helpers.” Here are the main ones you’ll actually use (and what they do):
__LINE__ // The current line number in the file
__FILE__ // The full path and filename
__DIR__ // The directory of the current file
__FUNCTION__ // The name of the current function
__CLASS__ // The name of the current class
__METHOD__ // The class and method name together
__NAMESPACE__ // The name of the current namespace
__TRAIT__ // The name of the current trait
ClassName::class // The full class name (including namespace)
A few quick demos:
echo __FILE__; // e.g. /var/www/html/functions.php
echo __LINE__; // shows the line number where this is written
function myFunction() {
echo __FUNCTION__; // prints "myFunction"
}
class MemberPressTools {
public function versionCheck() {
echo __METHOD__; // prints "MemberPressTools::versionCheck"
}
}
__DIR__ and __FILE__ are especially handy when working with includes or autoloaders — you can use them to safely reference other files in your project, no matter where the script runs from.
And yes — these constants are case-insensitive, so __LINE__ works the same as __line__.
Practical note:
When you’re debugging plugin code — say, a custom MemberPress add-on or a Pretty Links helper — these constants make it easy to trace exactly where a function or class is being executed. For example, logging __METHOD__ inside a hook callback can quickly tell you which part of the plugin triggered it, without manually searching through files.