Get the App
SLTechnology News&Howtos  ›  Development  › 

How to define constants in php

Shulou Source: shulou.com Published: 2022-06-01 18:39:03 10月04日 Update

This article mainly introduces "how to define constants in php". In daily operation, I believe many people have doubts about how to define constants in php. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to define constants in php". Next, please follow the editor to study!

Php definition of constant methods: 1, the use of define () function definition, syntax "define (constant name, constant value, whether case-sensitive)"; 2, the use of const keyword definition, syntax "const constant name = constant value;"

Operating environment of this tutorial: windows7 system, PHP7.1 version, DELL G3 computer

The method of defining constant in php

Define () function

Const keyword

Compared with variables, constants have the following characteristics:

The constant is not preceded by a dollar sign ($)

Constants can only be defined with define () and const

The scope of constants is global

Once a constant is defined, it cannot be redefined or undefined.

The define () function defines a constant

Legal constant names can only start with letters and underscores, followed by letters, numbers, or underscores of any length. You can define a constant through the define () function in PHP. The syntax of the define () function is as follows:

Define ($name, $value, $case_insensitive)

$name required parameter, constant name (need to be wrapped in double or single quotation marks)

$value required parameter, constant value

Optional parameter $case_insensitive, which specifies whether it is case-sensitive. By default, it is case-sensitive. Setting it to true indicates that it is case-insensitive.

Const keyword definition constant

Syntax:

Const constant name = constant value

Example:

The difference between define () and const:

Const defines constants at compile time, while the define () method defines constants at run time.

Const cannot be used in if statements, and defne () can be used in if statements.

If (...) {const FOO = 'BAR';// error} if (...) {define (' FOO', 'BAR'); / / correct}

A common scenario for define () is to determine whether a constant has been defined before defining a constant:

If (defined ('FOO)) {define (' FOO', 'BAR')}

When const defines a constant, the value can only be a static scalar (number, string, true,false, null), while the define () method can use the value of any expression as the value of a constant. Starting with PHP5.6, const also allows expressions to be used as constant values.

Const BIT_5 = 1

Tags: Constant function size method parameter syntax learning key keyword expression support underscore name scene letter quotation mark number is in more version Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Apple Linux Shulou Technology Shulou Tech Info