Web EndMemo


PHP Constants


Silimiar to C++, PHP use define keyword to declare a constant.

<?PHP
define("PI",3.1415926);
define("OOP", "Object Oriented Programming");
?>

Function constant() returns the value of a constant.
<?PHP
echo PI; //Print 3.1415926
echo "PI"; //Print PI
echo constant("PI");  //Print 3.1415926
?>


PHP built in constants list:
Constant Description
M_PI pi, ~3.1415926535898
M_E e, ~2.718281828459
_LINE_ Line number of current file
_FILE_ Full path name of current file
_DIR_ Directory of current file
_FUNCTION_ Name of current function
_CLASS_ Name of current class
_TRAIT_ Name of current trait
_METHOD_ Name of current class method
_NAMESPACE_ Name of current namespace
FALSE, TRUE, NULL
PHP_VERSION current php version, also PHP_MAJOR_VERSION, PHP_MINOR_VERSION, PHP_RELEASE_VERSION, PHP_VERSION_ID, PHP_EXTRA_VERSION
PHP_MAXPATHLEN Maximum length of filenames
PHP_OS Current operation system
PHP_SAPI Server API
PHP_EOL End of Line
PHP_INT_MAX Largest integer supported, similar PHP_INT_SIZE
DEFAULT_INCLUDE_PATH Current PHP search path
PEAR_INSTALL_DIR Install path of pear, similar PEAR_EXTENSION_DIR
PHP_EXTENSION_DIR PHP extension directory
PHP_BINDIR Directories where binaries installed, similiar PHP_MANDIR, PHP_LIBDIR, PHP_DATADIR, PHP_SYSCONFDIR, PHP_LOCALSTATEDIR, PHP_CONFIG_FILE_PATH, PHP_CONFIG_FILE_SCAN_DIR
PHP_SHLIB_SUFFIX Shared library suffix

PHP ERROR constants:
E_ERROR, E_WARNING, E_PARSE, E_NOTICE, E_CORE_ERROR, E_CORE_WARNING, E_COMPILE_ERROR, E_COMPILE_WARNING, E_USER_ERROR, E_USER_WARNING, E_USER_NOTICE, E_DEPRECATED, E_USER_DEPRECATED, E_ALL, E_STRICT.

Adjust error reporting using the ERROR constants:
<?PHP
error_reporting(0); //no error reporting
error_reporting(E_ALL); //report all errors
error_reporting(E_ALL ^ E_WARNING); //report all errors except E_WARNING
error_reporting(E_ERROR | E_WARNING); //report E_ERROR and E_WARNING
?>