Menu

PHP TUTORIALS - PHP Constants

PHP Constants

ADVERTISEMENTS

constant() example:


<?php

define("MINSIZE", 50);

echo MINSIZE;
echo constant("MINSIZE"); // same thing as the previous line

?>

ADVERTISEMENTS

Valid and invalid constant names:


// Valid constant names
define("ONE",     "first thing");
define("TWO2",    "second thing");
define("THREE_3", "third thing")
// Invalid constant names
define("2TWO",    "second thing");
define("__THREE__", "third value"); 

ADVERTISEMENTS

PHP Magic constants:

NameDescription
__LINE__The current line number of the file.
__FILE__The full path and filename of the file. If used inside an include,the name of the included file is returned. Since PHP 4.0.2, __FILE__ always contains an absolute path whereas in older versions it contained relative path under some circumstances.
__FUNCTION__The function name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the function name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
__CLASS__The class name. (Added in PHP 4.3.0) As of PHP 5 this constant returns the class name as it was declared (case-sensitive). In PHP 4 its value is always lowercased.
__METHOD__ The class method name. (Added in PHP 5.0.0) The method name is returned as it was declared (case-sensitive).