PHP Constants
Constants are variables whose values cannot be changed once defined.
Unlike normal variables, constants:
✅ Cannot be reassigned
✅ Do not use $ sign
✅ Usually store configuration values
✅ Are available globally
Why Use Constants?
Constants are useful for storing:
- Application Name
- Version Numbers
- Database Information
- API URLs
- Plugin Information
- Configuration Values
Creating Constants
PHP provides two ways:
define()const
Using define()
Syntax:
define("NAME", "VALUE");
Example:
define("SERVER_NAME", "ZyroNetwork");
echo SERVER_NAME;
Output:
ZyroNetwork
Using const
const VERSION = "1.0.0";
echo VERSION;
Output:
1.0.0
Difference Between define() and const
| Feature | define() | const |
|---|---|---|
| Runtime Definition | ✅ | ❌ |
| Inside Classes | ❌ | ✅ |
| Arrays Supported | ✅ | ✅ |
| Conditional Creation | ✅ | ❌ |
Naming Convention
Constants are usually written in:
UPPER_SNAKE_CASE
Example:
const PLUGIN_VERSION = "1.0.0";
const MAX_PLAYERS = 100;
Invalid Example
const version = "1.0";
This works but is not recommended.
Constants Are Immutable
const VERSION = "1.0";
VERSION = "2.0";
Output:
Fatal Error
Constants Are Global
const APP_NAME = "Zyro";
function test() {
echo APP_NAME;
}
Output:
Zyro
Constant Arrays
const RANKS = [
"Player",
"VIP",
"Admin"
];
print_r(RANKS);
Class Constants
class Server {
public const VERSION = "1.0";
}
Usage:
echo Server::VERSION;
Final Constants
final class Config {
public const VERSION = "1.0";
}
PocketMine Example
class Main extends PluginBase {
public const PREFIX =
"§6Zyro §8» ";
}
Usage:
$player->sendMessage(
self::PREFIX . "Welcome!"
);
Magic Constants
PHP provides predefined constants.
LINE
Returns current line number.
echo __LINE__;
Output:
25
FILE
Returns current file path.
echo __FILE__;
DIR
Returns current directory.
echo __DIR__;
FUNCTION
Returns function name.
function test() {
echo __FUNCTION__;
}
Output:
test
CLASS
Returns class name.
class User {
public function test() {
echo __CLASS__;
}
}
METHOD
Returns full method.
User::test
PHP Built-in Constants
PHP_VERSION
PHP_OS
PHP_INT_MAX
PHP_EOL
DIRECTORY_SEPARATOR
Example:
echo PHP_VERSION;
Real World Example
const API_URL =
"https://api.zyro.network";
const DATABASE_VERSION =
"2.1";
const DISCORD_INVITE =
"https://discord.gg/xxxxx";
Best Practices
✅ Use constants for configuration.
✅ Use uppercase names.
✅ Keep constants meaningful.
Bad Example
const A = 1;
const X = "abc";
Good Example
const SERVER_PORT = 19132;
const DEFAULT_LANGUAGE =
"en_US";
Exercises
Exercise 1
Create:
const NAME = "Aayan";
Exercise 2
Create:
const SERVER = "ZyroNetwork";
const VERSION = "1.0.0";
Exercise 3
Print:
PHP_VERSION
PHP_OS
Quiz
Can constants be changed?
Answer
No. Constants are immutable.
Which symbol is used?
Answer
Constants do NOT use $.
Challenge
Create a plugin information file:
const PLUGIN_NAME =
"ZyroCore";
const PLUGIN_VERSION =
"1.0.0";
const API_VERSION =
"5.0.0";
Useful Links
Official Documentation:
https://www.php.net/manual/en/language.constants.php
Magic Constants:
https://www.php.net/manual/en/language.constants.magic.php
Next Chapter
➡ PHP Data Types