PHP 8.5.2 Released!

define

(PHP 4, PHP 5, PHP 7, PHP 8)

defineDefinisce una costante

Descrizione

define(string $name, mixed $value, bool $case_insensitive = false): bool

Definisce una costante alfanumerica durante l'esecuzione.

Elenco dei parametri

name

Il nome della costante.

value

Il valore della costante; solo valori scalari e null sono consentiti. I valori scalari comprendono integer, float, string o boolean. E' anche possibile definire costanti di tipo resource, tuttavia non è raccomandato e può causare comportamenti imprevedibili.

case_insensitive

Se settato a true, la costante viene definita case-insensitive. Il comportamento di default è case-sensitive; per esempio CONSTANT e Constant rappresentano valori diversi.

Nota:

Le costanti case-insensitive vengono salvate internamente in minuscolo.

Valori restituiti

Restituisce true in caso di successo, false in caso di fallimento.

Esempi

Example #1 Definire costanti

<?php
define
("CONSTANT", "Ciao Mondo!");
echo
CONSTANT; // output "Ciao Mondo!"
echo Constant; // output "Ciao Mondo!" e genera un notice.

define("GREETING", "Ciao Mondo!", true);
echo
GREETING; // output "Ciao Mondo!"
echo Greeting; // output "Ciao Mondo!"

?>

Vedere anche:

  • defined() - Verifica se dato nome di una costante, essa esiste
  • constant() - Ritorna il valore di una costante
  • La sezione sulle Costanti

add a note

User Contributed Notes 4 notes

up
100
ravenswd at gmail dot com
10 years ago
Be aware that if "Notice"-level error reporting is turned off, then trying to use a constant as a variable will result in it being interpreted as a string, if it has not been defined.

I was working on a program which included a config file which contained:

<?php
define('ENABLE_UPLOADS', true);
?>

Since I wanted to remove the ability for uploads, I changed the file to read:

<?php
//define('ENABLE_UPLOADS', true);
?>

However, to my surprise, the program was still allowing uploads. Digging deeper into the code, I discovered this:

<?php
if ( ENABLE_UPLOADS ):
?>

Since 'ENABLE_UPLOADS' was not defined as a constant, PHP was interpreting its use as a string constant, which of course evaluates as True.
up
29
@SimoEast on Twitter
8 years ago
Not sure why the docs omit this, but when attempting to define() a constant that has already been defined, it will fail, trigger an E_NOTICE and the constant's value will remain as it was originally defined (with the new value ignored).

(Guess that's why they're called "constants".)
up
29
danbettles at yahoo dot co dot uk
16 years ago
define() will define constants exactly as specified.  So, if you want to define a constant in a namespace, you will need to specify the namespace in your call to define(), even if you're calling define() from within a namespace.  The following examples will make it clear.

The following code will define the constant "MESSAGE" in the global namespace (i.e. "\MESSAGE").

<?php
namespace test;
define('MESSAGE', 'Hello world!');
?>

The following code will define two constants in the "test" namespace.

<?php
namespace test;
define('test\HELLO', 'Hello world!');
define(__NAMESPACE__ . '\GOODBYE', 'Goodbye cruel world!');
?>
up
4
eparkerii at carolina dot rr dot com
17 years ago
Found something interesting.  The following define:

<?php
define("THIS-IS-A-TEST","This is a test");
echo THIS-IS-A-TEST;
?>

Will return a '0'.

Whereas this:

<?php
define("THIS_IS_A_TEST","This is a test");
echo THIS_IS_A_TEST;
?>

Will return 'This is a test'.

This may be common knowledge but I only found out a few minutes ago.

[EDIT BY danbrown AT php DOT net: The original poster is referring to the hyphens versus underscores.  Hyphens do not work in defines or variables, which is expected behavior.]
To Top