Sometimes in our code, we want to define variables with values that cannot be changed or defined during the execution of the script. For that purpose, we can use constants. Bare in mind that constants are case-sensitive and when declaring them are always uppercase.
Prior to PHP 8.0.0, when defining constants with define()
function, they may be case-insensitive.
Table of Contents
Syntax
There are two ways to define constants in PHP. You can use the const keyword or use the function define()
. Once you define a constant, it can't be changed or defined. Here is a simple example:
<?php
// Define a constant
define("NAME", "Hanna");
echo "My name is " . NAME . "\n";
const YEAR = 2000;
echo "Favorite year: " . YEAR . "\n";
const FRUITS = ['banana', 'orange', 'apple'];
echo "Favorite fruit: " . FRUITS[1] . "\n";
If you execute the script, the output of the above code will be:
My name is Hanna
Favorite year: 2000
Favorite fruit: orange
From the example, we can see that to access the value of the constant, we only need to specify its name. If we use an undefined constant, it will result in an error being thrown. For example:
echo NOT_CONSTANT;
If we try to execute the code above, because we have used a constant that doesn't exist it will throw a PHP warning that will inform you that you have used undefined constant.
To dodge these types of errors you can use the defined()
function, that checks whether a constant exists with that name. This function returns true if the constant with that name has been defined, false otherwise. Here is an example of using the defined()
function.
<?php
// check if a constant exists
if(defined("SOME_CONSTANT"))
echo "The constant exists.";
To get a list of all defined constants you could use the function get_defined_constants() .
<?php
var_dump(get_defined_constants());
Magic constants
PHP provides a set of predefined constants that change depending on where they are used. The magic constants are case-insensitive. They are resolved at runtime. These are all of them:
Name | Description |
---|---|
__LINE__ |
Returns the current line number of the file. |
__FILE__ |
Returns the full path and the filename of the file. |
__DIR__ |
Returns the name of the directory of the file. |
__FUNCTION__ |
Returns the name of the function or {closure} for anonymous functions |
__CLASS__ |
Returns the class name, also includes the namespace. |
__TRAIT__ |
Returns the trait name, also includes the namespace. |
__METHOD__ |
Returns the class method name. |
__NAMESPACE__ |
Returns the name of the current namespace. |
In the examples below are shown all of the PHP magic constants and its use.
<?php
// __FILE__
echo "Full file path: " . __FILE__ . "\n";
// __DIR__
echo "Path to the directory: " . __DIR__ . "\n";
//__FUNCTION__
function example(){
echo "Function name is " . __FUNCTION__. "\n";
}
example();
<?php
namespace ExampleNamespace;
class ExampleClass {
public function getClassName(){
return __CLASS__;
}
public function oneExampleMethod(){
return __METHOD__;
}
public function getNamespace(){
return __NAMESPACE__;
}
}
$obj = new ExampleClass();
echo "Class name: ". $obj->getClassName() . "\n";
echo "Method name: " . $obj->oneExampleMethod() . "\n";
echo "Namespace: " . $obj->getNamespace() . "\n";
Conclusion
In this article, you got to explore the constants. Through examples you learned how to define a constant and what are the magic constants. If you would like to learn more about constants you can check the official PHP documentation. Also the code of this tutorial is available on my Github.