Magic Constants in PHP : Tutorial



A constant is a value that does not change durring the execution of a PHP script.

Constant's are case-sensitive.
Constant's should always be declared in uppercase (by convention).
The name of a constant follows the same rules as any label in PHP ie, name of constants must starts with a letter or underscore followed by any number of letters, numbers, or underscores.
Syntax:
bool define (string $name, mixed $value [,bool $case_insensitive = false]);
define function returns a bool value, it has 3 parameters (3rd one is optional)
1st parameter is a string value Constant name.
2nd parameter is the constant value.
3rd parameter is a case_insensitive bool which is optional and has a value false if not defined.


Example 1:
<?php 

//By default Case-insensitive parameter is set as FALSE
define("PI", "3.14.<br>"); 

echo PI; //Correct way to output constants
echo pi; // Wrong! PHP constants are case SENSITIVE!


define("MESSAGE", "Hello there!<br>", true);
echo MESSAGE;
echo message; // Works!! as Case-insensitive parameter is set as true

?>
Output

3.14. Notice: Use of undefined constant pi - assumed 'pi' in I:\xampp\htdocs\xampp\Tutorials\ConstantsInPHP.php on line 7 piHello there! Hello there!


Example 2:
<?php 

define("3D", "Three Dimention.
"); echo 3D; //Will give a parse-error ?>
Output

Parse error: syntax error, unexpected 'D' (T_STRING), expecting ',' or ';' in I:\xampp\htdocs\xampp\Tutorials\ConstantsInPHP.php on line 4

As seen in the above example that when goto is used to jump into a loop an Fatal error is thrown and the script is terminated!.
Scope of a constant is global. We can access constants anywhere in the script without regard to scope.


Predefined constants in PHP.

PHP has a list of predefined constants. Some of which are,

1. PHP_VERSION

   -Displays the current version of PHP being installed
 
2. PHP_MAJOR_VERSION

   -Displays the current major version of PHP being installed
  
3. PHP_MINOR_VERSION

   -Displays the current minor version of PHP being installed

4. PHP_RELEASE_VERSION

   -Displays the current release version of PHP being installed

5. PHP_VERSION_ID

   -Displays the current version id of PHP being installed its an integer value.

6. PHP_EXTRA_VERSION

   -Displays the extra version of PHP being installed its an integer value.

7. PHP_MAXPATHLEN

   -Displays maximum length of filenames (including path) supported by this build of PHP.

8. PHP_SAPI

   -Displays Server API for this build of PHP.

9. PHP_EOL

   -Displays 'End Of Line' symbol for the current platform.

10. PHP_SHLIB_SUFFIX

	-Displays build-platform's shared library suffix, such as "so" (most Unixes) or "dll" (Windows).

11. PHP_INT_MAX 

    -Displays the largest integer supported in this build of PHP. Usually int(2147483647).
    
12. PHP_BINARY (string)

    -Displays the PHP binary path during script execution.


Some more!...


PHP_ZTS 
PHP_DEBUG
PHP_OS
PHP_INT_SIZE
DEFAULT_INCLUDE_PATH
PEAR_INSTALL_DIR
PEAR_EXTENSION_DIR
PHP_EXTENSION_DIR
PHP_PREFIX
PHP_BINDIR
PHP_MANDIR
PHP_LIBDIR
PHP_DATADIR
PHP_SYSCONFDIR
PHP_LOCALSTATEDIR
PHP_CONFIG_FILE_PATH
PHP_CONFIG_FILE_SCAN_DIR
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
TRUE
FALSE
NULL
<?php 

echo "PHP_VERSION = ".PHP_VERSION."<br>";
echo "PHP_MAJOR_VERSION = ".PHP_MAJOR_VERSION."<br>";
echo "PHP_MINOR_VERSION = ".PHP_MINOR_VERSION."<br>";
echo "PHP_RELEASE_VERSION = ".PHP_RELEASE_VERSION."<br>";
echo "PHP_VERSION_ID = ".PHP_VERSION_ID."<br>";
echo "PHP_EXTRA_VERSION = ".PHP_EXTRA_VERSION."<br>";
echo "PHP_MAXPATHLEN = ".PHP_MAXPATHLEN."<br>";
echo "PHP_SAPI = ".PHP_SAPI."<br>";
echo "PHP_EOL = ".PHP_EOL."<br>";
echo "PHP_SHLIB_SUFFIX = ".PHP_SHLIB_SUFFIX."<br>";
echo "PHP_INT_MAX = ".PHP_INT_MAX."<br>";
echo "PHP_BINARY = ".PHP_BINARY."<br>";
echo "PHP_ZTS = ".PHP_ZTS."<br>";
echo "PHP_DEBUG = ".PHP_DEBUG."<br>";
echo "PHP_OS = ".PHP_OS."<br>";
echo "PHP_INT_SIZE = ".PHP_INT_SIZE."<br>";
echo "DEFAULT_INCLUDE_PATH = ".DEFAULT_INCLUDE_PATH."<br>";
echo "PEAR_INSTALL_DIR = ".PEAR_INSTALL_DIR."<br>";
echo "PEAR_EXTENSION_DIR = ".PEAR_EXTENSION_DIR."<br>";
echo "PHP_EXTENSION_DIR = ".PHP_EXTENSION_DIR."<br>";
echo "PHP_PREFIX = ".PHP_PREFIX."<br>";
echo "PHP_BINDIR = ".PHP_BINDIR."<br>";
echo "PHP_LIBDIR = ".PHP_LIBDIR."<br>";
echo "PHP_DATADIR = ".PHP_DATADIR."<br>";
echo "PHP_SYSCONFDIR = ".PHP_SYSCONFDIR."<br>";
echo "PHP_LOCALSTATEDIR = ".PHP_LOCALSTATEDIR."<br>";
echo "PHP_CONFIG_FILE_PATH =".PHP_CONFIG_FILE_PATH."<br>";
echo "PHP_CONFIG_FILE_SCAN_DIR = ".PHP_CONFIG_FILE_SCAN_DIR."<br>";
echo "E_ERROR = ".E_ERROR."<br>";
echo "E_WARNING = ".E_WARNING."<br>";
echo "E_PARSE = ".E_PARSE."<br>";
echo "E_NOTICE = ".E_NOTICE."<br>";
echo "E_CORE_ERROR = ".E_CORE_ERROR."<br>";
echo "E_CORE_WARNING = ".E_CORE_WARNING."<br>";
echo "E_COMPILE_ERROR = ".E_COMPILE_ERROR."<br>";
echo "E_COMPILE_WARNING = ".E_COMPILE_WARNING."<br>";
echo "E_USER_ERROR = ".E_USER_ERROR."<br>";
echo "E_USER_WARNING = ".E_USER_WARNING."<br>";
echo "E_USER_NOTICE = ".E_USER_NOTICE."<br>";
echo "E_DEPRECATED = ".E_DEPRECATED."<br>";
echo "E_USER_DEPRECATED = ".E_USER_DEPRECATED."<br>";
echo "E_ALL = ".E_ALL."<br>";
echo "E_STRICT = ".E_STRICT."<br>";
echo "TRUE = ".TRUE."<br>";
echo "FALSE = ".FALSE."<br>";
echo "NULL = ".NULL."<br>";
?>
Output

PHP_VERSION = 5.4.7 PHP_MAJOR_VERSION = 5 PHP_MINOR_VERSION = 4 PHP_RELEASE_VERSION = 7 PHP_VERSION_ID = 50407 PHP_EXTRA_VERSION = PHP_MAXPATHLEN = 260 PHP_SAPI = apache2handler PHP_EOL = PHP_SHLIB_SUFFIX = dll PHP_INT_MAX = 2147483647 PHP_BINARY = I:\xampp\apache\bin\httpd.exe PHP_ZTS = 1 PHP_DEBUG = 0 PHP_OS = WINNT PHP_INT_SIZE = 4 DEFAULT_INCLUDE_PATH = .;C:\php\pear PEAR_INSTALL_DIR = C:\php\pear PEAR_EXTENSION_DIR = C:\php PHP_EXTENSION_DIR = C:\php PHP_PREFIX = C:\php PHP_BINDIR = C:\php PHP_LIBDIR = C:\php PHP_DATADIR = C:\php PHP_SYSCONFDIR = C:\php PHP_LOCALSTATEDIR = C:\php PHP_CONFIG_FILE_PATH =C:\Windows PHP_CONFIG_FILE_SCAN_DIR = E_ERROR = 1 E_WARNING = 2 E_PARSE = 4 E_NOTICE = 8 E_CORE_ERROR = 16 E_CORE_WARNING = 32 E_COMPILE_ERROR = 64 E_COMPILE_WARNING = 128 E_USER_ERROR = 256 E_USER_WARNING = 512 E_USER_NOTICE = 1024 E_DEPRECATED = 8192 E_USER_DEPRECATED = 16384 E_ALL = 32767 E_STRICT = 2048 TRUE = 1 FALSE = NULL =