|
|
 |
define (PHP 3, PHP 4, PHP 5) define -- Defines a named constant Descriptionbool define ( string name, mixed value [, bool case_insensitive] )
Defines a named constant. See the
section on constants
for more details.
The name of the constant is given by name;
the value is given by value.
The optional third parameter
case_insensitive is also available. If the
value TRUE is given, then the constant will be
defined case-insensitive. The default behaviour is
case-sensitive; i.e. CONSTANT and Constant represent different
values.
Example 1. Defining Constants |
<?php
define("CONSTANT", "Hello world.");
echo CONSTANT; echo Constant; define("GREETING", "Hello you.", true);
echo GREETING; echo Greeting; ?>
|
|
Returns TRUE on success or FALSE on failure.
See also defined(),
constant() and the section on Constants.
User Contributed Notes
define
dsan at pml dot ac dot uk
08-Apr-2005 10:17
Just to clarify what 'david at nospam webgroup dot org' said:
${} is actually the syntax for 'variable variables'.
So in this specific case:
Starting with the line:
${MY_CONST} = serialize(array('foo' => 'bar'));
PHP will expand the constant and use it as the name of the variable:
$test = serialize(array('foo' => 'bar'));
So in fact there is no assignment to a constant (which would defy the definition of 'constant') but instead an assignment to a variable named by the _value_ of the constant.
david at nospam webgroup dot org
31-Mar-2005 05:14
To the comment posted by Raphael Crawford Marks:
You're actually manipulating the variable $test. The value of a constant obviously cannot be changed after it is set: hence the term constant. Try setting $test to something first, then running your code. It *WILL* be clobbered.
Raphael Crawford-Marks
10-Mar-2005 01:28
Notes on using serialize to store an array in a constant:
<?php
define('MYCONST','test'); ${MYCONST} = serialize(array("foo" => "bar")); echo "constant: ".${MYCONST};
echo "<br>";
$unserialized = unserialize(${MYCONST}); echo "unserialized: ".$unserialized['foo'];
?>
Note that you have to use ${} around the constant name for this to work.
11-Feb-2005 10:45
Better pack with define() for all who really miss Java package management:
Use this "manifest.php" on very first script start or copy it to your config.somehow.php.
<?php
$__packages = array(
"org.ilove.java.more",
"org.ilove.python.too",
"net.php.isok"
);
define("C_IS_WINDOWS", false);
define("C_DIR", (C_IS_WINDOWS ? "\\" : "/"));
define("C_PATH_ROOT", str_replace("/", C_DIR, $_SERVER["DOCUMENT_ROOT"]).C_DIR);
define("C_PATH_CORE", C_PATH_ROOT."core".C_DIR);
define("C_PATH_CLASS", C_PATH_CORE."classes".C_DIR);
define("C_APPLICATION_BASE", C_PATH_CORE.C_DIR."application".C_DIR);
$total_packages = 0;
$i = sizeof($__packages);
while($i-- > 0) {
$tokens = explode(".", $__packages[$i]);
$j = sizeof($tokens);
while($j-- > 0) {
$token = strtolower(trim($tokens[$j]));
if(strlen($token) > 0 && !defined($token)) {
define($token, ($j == 0 ? C_PATH_CLASS : "").$tokens[$j].C_DIR);
$total_packages++;
}
}
}
define("C_PACKAGE_COUNT", $total_packages);
?>
With restrictions on non-package constants, you now can call your files like that:
<?php
require_once org.ilove.java.more."Than.php";
?>
Regards
Robi
tech at litigationdataservices dot com
25-Jan-2005 03:05
If your oft-used array is already defined, but the key value doesn't start at ['0'], --for example if you need an array of state names conforming to GIS codes, it's far easier to define the array in a low-level class and call it by
$classname->states[$STATEID];
than it is to try to define it as a constant or use an function to call it later from a free-standing array. It's also useful to do it this way when all you want to do is recall a fixed array value, but don't necessarily want to hit the database just to get it.
technopasta at yahoo dot com dot au
02-Jan-2005 07:55
You can't use arrays in constants, so I came up with this two line function to get around it.
<?php
function const_array($constant) {
$array = explode(",",$constant);
return $array;
};
?>
So now if you define a constant like
<? define('myconstant','item1,item2,item3') ?>
and then use my function
<? $myarray = const_array(myconstant); ?>
$myarray will now contain an array with
item1
item2
item3
Hope this is useful for someone...
konstantin #at schukraft #dot org
22-Sep-2003 05:13
Constants MUST evaluate to scalar values only.
You are encouraged to use serialize/unserlialize
to store/retrieve an array in a constant:
define('CONST',serialize(array('a','b','foo'=>'bar')));
var_dump(CONST);
var_dump(unserialize(CONST));
phpnet at trenkner dot de
14-Mar-2003 05:59
---[Editor's Note]---
As of PHP 5.0.0 this is possible. You can define class-only constants, which can be called like Foo::Constant1 from the outside
---[End Note]---
Please keep in mind that
class AClass {
define ("Const1", "Value1");
... }
didn't work. You have to make all your constant definitions before you open the class. So
define ("Const1", "Value1");
class AClass {
... }
would be correct.
radovan dot biciste at managestar dot com
06-Nov-2001 10:45
Wonder how to work with variable which name is stored in a constant?
Here it is:
<?php
define("VAR_NAME","test");
${VAR_NAME} = "value";
echo ${VAR_NAME};
?>
ste at opk dot no
29-Aug-2001 12:41
To use a constant to show an element of an array inside a string:
define ('C', 0); print ("element 0: {$a[C]}");
The { & } around the variable signals that what's inside should be treated as a variable and not a string.
Note that 'print ("a constant:{C}");' wont work as ZERO is a constant.
| |