search for in the  
<connection_timeoutdefine>
Last updated: Thu, 19 May 2005

constant

(PHP 4 >= 4.0.4, PHP 5)

constant -- Returns the value of a constant

Description

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.

Example 1. constant() example

<?php

define
("MAXSIZE", 100);

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

?>

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



User Contributed Notes
constant
thisisroot at gmail dot com
02-Feb-2005 09:34
FYI, this function works with class constants too.

Below is an example in which a database abstraction layer creates connections to different relational database systems based on class constants such as DBFission::CONNECTION_MYSQL, DBFission::CONNECTION_POSTGRESQL, etc. Using this function, I can convert text in an INI file to an integer constant easily.

<?php
// php voodoo to get a class constant based on a string
$type = constant( 'DBFission::CONNECTION_' . strtoupper( $c[ 'type']));

?>
Joachim Kruyswijk
13-Nov-2004 11:12
The constant name can be an empty string.

Code:

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

Output:

foo
mina86 at tlen dot pl
04-Oct-2003 05:10
re PHPdotNET at 19-Oct-2002 08:02:
it is better tu use (no warning is generated so you don't have to use '@'):
<?php
if (!defined("<file-id>")) {
define("<file-id>", true);

// script content

}
?>
or
<?php
if (defined("<file-ide">)) return;

// script content
?>

You could also consider using include_once() and/or require_once();
VGR_experts_exchange at edainworks dot com
19-Sep-2003 07: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 :

<?php
if ((defined('TRACE'))AND(constant('TRACE')))  {}
?>
Andre
27-Apr-2003 03:10
Maybe this is useful:

$file_ext is the file Extension of the image

<?php
if ( imagetypes() & @constant('IMG_' . strtoupper($file_ext)) )
{
  
$file_ext = $file_ext == 'jpg' ? 'jpeg' : $file_ext;
  
$create_func = 'ImageCreateFrom' . $file_ext;
}
?>
23-Aug-2002 06:11
Think of this function as the constant version of "variable variables".

<connection_timeoutdefine>
 Last updated: Thu, 19 May 2005
Copyright © 2001-2005 The PHP Group
All rights reserved.
This unofficial mirror is operated at: The Server Pages
Last updated: Thu May 19 17:35:34 2005 CDT