Current Location: Home> Function Categories> define

define

Define a constant
Name:define
Category:Miscellaneous
Programming Language:php
One-line Description:Define constants.

Definition and usage

define() function defines a constant.

Constants are similar to variables, the difference is:

  • The value of a constant cannot be changed after setting
  • The constant name does not require a dollar sign ($) that begins with
  • Constants can be accessed within any scope
  • The value of a constant can only be strings and numbers

Example

Example 1

Define a case-sensitive constant:

 <?php
define ( "GREETING" , "Hello world!" ) ;
echo constant ( "GREETING" ) ;
?>

Try it yourself

Example 2

Define a case-insensitive constant:

 <?php
define ( "GREETING" , "Hello world!" , TRUE ) ;
echo constant ( "greeting" ) ;
?>

Output:

 Hello world!

grammar

 define ( name , value , case_insensitive )
parameter describe
name Required. Specifies the name of the constant.
value Required. Specifies the value of the constant.
case_insensitive

Optional. Specifies whether the constant name is case-insensitive. Possible values:

  • TRUE - Case insensitive (deprecated in PHP 7.3)
  • FALSE - case sensitive (this is the default)

illustrate

After PHP 7.3, it is not recommended to use case-insensitive constant names because this option has been deprecated. When defining constants, a case-sensitive name should always be used.

Additionally, while PHP 7 allows arrays to be used as values ​​for constants, this is not a typical use of constants, as constants are often used to store simple invariant values ​​such as configuration settings or constant expressions.

In PHP 5, the value of a constant must be a scalar type (such as a string, integer, floating point number) or a Boolean value, or NULL.

Similar Functions