Watch out. You can define a new constant with the name NULL with define("NULL","FOO");. But you must use the function constant("NULL"); to get it's value. NULL without the function call to the constant() function will still retrieve the special type NULL value.
Within a class there is no problem, as const NULL="Foo"; will be accessible as myClass::NULL.
define
(PHP 4, PHP 5)
define — Definiert eine benannte Konstante
Beschreibung
Definiert während der Laufzeit eine benannte Konstante.
Parameter-Liste
-
name -
Der Name der Konstante
-
value -
Der Wert der Konstante. Es sind nur Skalar- und NULL-Werte erlaubt. Skalarwerte sind Ganzzahlen, Gleitkommazahlen, Zeichenketten oder boolsche Werte. Es ist möglich, Konstanten vom Typ Ressource zu definieren, allerdings wird dies nicht empfohlen, da es unvorhersagbares Verhalten des Programms zur Folge haben kann.
-
case_insensitive -
Falls auf
TRUEgesetzt, wird bei der Konstante nicht zwischen Groß- und Kleinschreibung unterschieden. In der Voreinstellung wird zwischen Groß- und Kleinschreibung unterschieden, d.h KONSTANTE und Konstante repräsentieren unterschiedliche Werte.Hinweis:
Groß-/Kleinschreibungsunabhängige Konstanten werden kleingeschrieben gespeichert.
Rückgabewerte
Gibt bei Erfolg TRUE zurück. Im Fehlerfall wird FALSE zurückgegeben.
Beispiele
Beispiel #1 Konstanten definieren
<?php
define("KONSTANTE", "Hallo Welt.");
echo KONSTANTE; // gibt "Hallo Welt." aus
echo Konstante; // gibt "Konstante" aus und erzeugt eine Benachrichtigung
define("BEGRUESSUNG", "Hallo Du.", true);
echo BEGRUESSUNG; // gibt "Hallo Du." aus
echo Begruessung; // gibt "Hallo Du." aus
?>
Siehe auch
- defined() - Prüft, ob eine benannte Konstante existiert
- constant() - Liefert den Wert einer Konstante
- Das Kapitel über Konstanten
define
09-Jul-2007 07:34
03-Jun-2007 11:19
chris at frecod dot de, you might just use SQL for this:
NOT IN(var1,var2,var3,var4,var5,var6)
01-Jan-2007 03:59
An improvement on the function from bobbykjack at yahoo dot co dot uk on the concept by richard dot quadling at bandvulc dot co dot uk:
<?php
function adefine($constant_name, $value=null) {
static $increment = 0; // 1 for bitmask
if (is_null($value)) {
define($constant_name, ++$increment); // $increment=$increment<<1 for bitmask
} else {
define($constant_name, $value);
if (is_numeric($value)) {
$increment = $value;
}
}
}
?>
If you pass it a second argument it defines it normally, and resets the increment if the value is numeric. This way the function can replace define, and you can reset the counter for a new set of constants.
<?php
adefine ('RULE_CALLBACK_FORMAT', 1); // 1
adefine ('RULE_CHANGE_CALLBACK_ON_ERROR'); // 2
adefine ('RULE_CHANGE_COMPARE_DATE'); // 3
adefine('KEYWORD', 'hodgepodge'); // hodgepodge <-- defined normally
adefine ('RULE_CHANGE_ON_DATE'); // 4
adefine ('ERROR_DESC', 1); // 1 <-- Counter reset
adefine ('ERROR_EXPECTED_RESULT'); // 2
?>
15-Nov-2005 03:42
[Editor's Note: Obviously, constants cannot be redefined. That is the meaning of a constant.]
Just a quick note.. If a constant is once defined, any subsequent attempts to define it once again are ignored.
<?
define('WHAT_DID_YOU_EXPECT', 'First');
define('WHAT_DID_YOU_EXPECT', 'Second');
echo WHAT_DID_YOU_EXPECT
?>
Outputs 'First'.
I really thought I have gone mad when I saw just the last two lines of code (the first one was in another file) and it was echoing 'First'..
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
15-Mar-2003 12: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.
07-Nov-2001 05:45
Wonder how to work with variable which name is stored in a constant?
Here it is:
<?php
define("VAR_NAME","test");
// assigning value
${VAR_NAME} = "value";
// getting value back
echo ${VAR_NAME};
?>
29-Aug-2001 07: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.