(PHP 4 >= 4.0.4, PHP 5)

constant -- Returns the value of a constant


mixed constant ( string name )

constant() will return the value of the constant indicated by name.

constant() is useful if you need to retrieve the value of a constant, but do not know its name. i.e. It is stored in a variable or returned by a function.

例子 1. constant() example


("MAXSIZE", 100);

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


See also define(), defined() and the section on Constants.

add a note add a note User Contributed Notes
04-Oct-2006 08:17
If the constant does not exist, constant() will generate a warning and return null.
narada dot sage at googlemail dot com
13-Jul-2006 09:01
To access the value of a class constant use the following technique.


class a {
b = 'c';


// output: c

service at dual-creators dot de
17-May-2006 01:00
It's easily to user constant() and define() to translate some words from your database-saves.

For example:
You have a table userprofil and one coloumn is "gender".
Gender can be male or female but you will display "maennlich" or "weiblich" (german words for it - whatever...)

First step: Fetch into $Gender

define("male", "maennlich");
define("female", "weiblich");

echo constant($Gender);

Now, the index of the variable $Gender will be handled like a constant!

(It works like "echo male;" for better understanding)

And a result of this, it displays maennlich btw. weiblich!

Trevor Blackbird >
19-Apr-2006 05:58
Technically you can define constants with names that are not valid for variables:


// $3some is not a valid variable name
// This will not work
$3some = 'invalid';

// This works
define('3some', 'valid');


Of course this is not a good practice, but PHP has got you covered.
timneill at hotmail dot com
26-Nov-2005 04:39
Please note when using this function from within a class to retrieve a php5 class constant, ensure you include the 'self::'.

class Validate
   const TEXT_MAX = 65536;
   //-- this will work
   public static function textWORKS($_value, $_type = 'TEXT')
       $_max = constant('self::' . $_type . '_MAX');
       return (strlen($_value) <= $_max ? true : false);
   //-- this will fail
   public static function textFAILS($_value, $_type = 'TEXT')
       //-- Debug Warning: constant(): Couldn't find constant TEXT_MAX
       $_max = constant($_type . '_MAX');
       return (strlen($_value) <= $_max ? true : false);
11-Oct-2005 10:20
In reply to VGR_experts_exchange at edainworks dot com

To check if a constant is boolean, use this instead:

if (TRACE === true)  {}

Much quicker and cleaner than using defined() and constant() to check for a simple boolean.

IMO, using ($var === true) or ($var === false) instead of ($var) or (!$var) is the best way to check for booleans no matter what. Leaves no chance of ambiguity.
Joachim Kruyswijk
14-Nov-2004 01:12
The constant name can be an empty string.


define("", "foo");
echo constant("");


VGR_experts_exchange at edainworks dot com
19-Sep-2003 08:32
Hello. This applies to constants being defined as Boolean values, and may-be applies generally.

I advise you to NOT use this in an included file, in a function or elsewhere outside the scope where the define('TRACE',TRUE) is placed) :

if (TRACE) {}

This will always evaluate to TRUE if the constant is not defined previously (the story about this becoming an string 'TRACE', thus evaluating to TRUE)

Use this :

if ((defined('TRACE'))AND(constant('TRACE')))  {}
28-Apr-2003 04:10
Maybe this is useful:

$file_ext is the file Extension of the image

if ( imagetypes() & @constant('IMG_' . strtoupper($file_ext)) )
$file_ext = $file_ext == 'jpg' ? 'jpeg' : $file_ext;
$create_func = 'ImageCreateFrom' . $file_ext;