Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

PHP Dictionary

  1. Home
  2. PHP Dictionary
  3. define() / defined() / const

define() / defined() / const Since: PHP 4(2000)

A mechanism for defining values that do not change during the program's execution. Used to manage fixed values such as configuration settings and version numbers.

Syntax

// Defines a constant at runtime.
define(name, value);

// Checks whether a constant has been defined.
defined(name);

// Defines a constant at compile time.
const name = value;

// Returns all currently defined constants.
get_defined_constants($categorize);

Syntax / Functions

Syntax / FunctionDescription
define($name, $value)Defines a constant at runtime. Can be used inside conditional blocks, and the constant name can be specified with a variable.
defined($name)Checks whether a constant with the given name has been defined. Returns true if defined, or false if not.
constA keyword that defines a constant at compile time. Can be used at the top level or inside a class, but not inside conditional blocks or functions.
get_defined_constants($categorize)Returns all defined constants as an array. Passing true as the argument returns the constants grouped by category.

PHP Built-in Constants

ConstantDescription
PHP_VERSIONReturns the currently running PHP version as a string.
PHP_INT_MAXThe maximum integer value. On 64-bit systems, this is 9223372036854775807.
PHP_EOLThe platform-specific line ending character. \n on Linux, \r\n on Windows.
PHP_OSThe name of the operating system PHP is running on.
DIRECTORY_SEPARATORThe directory separator character. / on Linux, \ on Windows.
__FILE__The full path of the current file. One of the magic constants.
__DIR__The directory path of the current file.
__LINE__The current line number.

Sample Code

<?php
// Define constants using define().
define('SITE_NAME', 'wp-p.info');
define('MAX_UPLOAD_SIZE', 10 * 1024 * 1024); // 10MB
echo SITE_NAME; // Outputs 'wp-p.info'.

// Define top-level constants using const.
const APP_VERSION = '2.0.0';
const DEBUG_MODE = false;
echo APP_VERSION; // Outputs '2.0.0'.

// Check whether a constant exists using defined().
if (!defined('TIMEZONE')) {
	define('TIMEZONE', 'Asia/Tokyo');
}
echo TIMEZONE; // Outputs 'Asia/Tokyo'.

// define() can be used inside conditionals, but const cannot.
if (getenv('APP_ENV') === 'production') {
	define('BASE_URL', 'https://wp-p.info');
} else {
	define('BASE_URL', 'http://localhost:8080');
}

// As of PHP 7, define() supports array constants.
define('SUPPORTED_LANGUAGES', ['PHP', 'JavaScript', 'Python']);
echo SUPPORTED_LANGUAGES[0]; // Outputs 'PHP'.

// Defining class constants.
class HttpStatus {
	public const OK = 200;
	public const NOT_FOUND = 404;
	public const SERVER_ERROR = 500;

	public static function message(int $code): string {
		return match ($code) {
			self::OK => 'OK',
			self::NOT_FOUND => 'Not Found',
			self::SERVER_ERROR => 'Internal Server Error',
			default => 'Unknown',
		};
	}
}

echo HttpStatus::NOT_FOUND; // Outputs '404'.
echo HttpStatus::message(200); // Outputs 'OK'.

// Using magic constants.
echo "File: " . __FILE__ . PHP_EOL;
echo "Directory: " . __DIR__ . PHP_EOL;
echo "Line: " . __LINE__ . PHP_EOL;
echo "PHP version: " . PHP_VERSION . PHP_EOL;

Notes

Both define() and const define constants, but they differ in where they can be used. const is evaluated at compile time, so it cannot be used inside conditional blocks; define() is evaluated at runtime, so it can be used anywhere. Use const to define fixed values at the top level, and use define() when the value needs to be determined conditionally.

To define constants inside a class, use the const keyword. As of PHP 8.1, you can use enum to group related constants in a more type-safe way.

Magic constants are special constants surrounded by double underscores on each side, and their values change depending on where they appear in the code. They are useful for retrieving file names and line numbers during debugging, and for building absolute file paths. For access modifiers and class constants, see public / private / protected.

If you find any errors or copyright issues, please .